Insertion des documents

L'insertion de données dans une base MongoDB se fait avec l'instruction db.collection.insert(Document JSON).

Si la collection n'existe pas, elle est créée dynamiquement.

L'insertion associe un identifiant (_id) à chaque document de la collection.

Exemple

CTRL+C pour copier, CTRL+V pour coller
1
db.Cinema.insert(
2
{
3
   "nom":"Honkytonk Man",
4
   "realisateur":{
5
      "nom":"Eastwood",
6
      "prenom":"Clint"
7
   },
8
   "annee":1982,
9
   "acteurs":[
10
      {
11
         "nom":"Eastwood",
12
         "prenom":"Kyle"
13
      },
14
      {
15
         "nom":"Eastwood",
16
         "prenom":"Clint"
17
      }
18
   ]
19
}
20
)
db.Cinema.insert(
{
   "nom":"Honkytonk Man",
   "realisateur":{
      "nom":"Eastwood",
      "prenom":"Clint"
   },
   "annee":1982,
   "acteurs":[
      {
         "nom":"Eastwood",
         "prenom":"Kyle"
      },
      {
         "nom":"Eastwood",
         "prenom":"Clint"
      }
   ]
}
)