MongoDB Basics Part 2

In this article, we will continue to work with MongoDB by interacting with the MongoDB shell.

Insert Document


The db.collection.insert() method inserts new documents to a collection.

> db.inventory.insert(
...    {
...      item: "ABC1",
...      details: {
...         model: "14Q3",
...         manufacturer: "XYZ Company"
...      },
...      stock: [ { size: "S", qty: 25 }, { size: "M", qty: 50 } ],
...      category: "clothing"
...    }
... )
WriteResult({ "nInserted" : 1 })

The result is a WriteResult object that shows the number of documents inserted.

Read Document


> db.inventory.find()
{ "_id" : ObjectId("5425a7730b73fd80bdd0724c"), "item" : "ABC1",
"details" : { "model" : "14Q3", "manufacturer" : "XYZ Company" },
"stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ], "category" : "clothing" }

Insert Multiple Documents


Create an array that holds multiple documents to insert.

> var mydocuments =
...     [
...       {
...         item: "ABC2",
...         details: { model: "14Q3", manufacturer: "M1 Corporation" },
...         stock: [ { size: "M", qty: 50 } ],
...         category: "clothing"
...       },
...       {
...         item: "MNO2",
...         details: { model: "14Q3", manufacturer: "ABC Company" },
...         stock: [ { size: "S", qty: 5 }, { size: "M", qty: 5 }, { size: "L", qty: 1 } ],
...         category: "clothing"
...       },
...       {
...         item: "IJK2",
...         details: { model: "14Q2", manufacturer: "M5 Corporation" },
...         stock: [ { size: "S", qty: 5 }, { size: "L", qty: 1 } ],
...         category: "houseware"
...       }
...     ];

Insert the documents.

> db.inventory.insert(mydocuments)
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})

Query Documents


Select all documents in a collection.

> db.inventory.find()
{ "_id" : ObjectId("5425a7730b73fd80bdd0724c"), "item" : "ABC1",
"details" : { "model" : "14Q3", "manufacturer" : "XYZ Company" },
"stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ], "category" : "clothing" }
{ "_id" : ObjectId("5425a9080b73fd80bdd0724d"), "item" : "ABC2",
"details" : { "model" : "14Q3", "manufacturer" : "M1 Corporation" },
"stock" : [ { "size" : "M", "qty" : 50 } ], "category" : "clothing" }
{ "_id" : ObjectId("5425a9080b73fd80bdd0724e"), "item" : "MNO2",
"details" : { "model" : "14Q3", "manufacturer" : "ABC Company" },
"stock" : [ { "size" : "S", "qty" : 5 }, { "size" : "M", "qty" : 5 }, { "size" : "L", "qty" : 1 } ], "category" : "clothing" }
{ "_id" : ObjectId("5425a9080b73fd80bdd0724f"), "item" : "IJK2",
"details" : { "model" : "14Q2", "manufacturer" : "M5 Corporation" },
"stock" : [ { "size" : "S", "qty" : 5 }, { "size" : "L", "qty" : 1 } ], "category" : "houseware" }

Specify Equality Condition

To retrieve from the inventory collection all documents where the category field is houseware:

> db.inventory.find({category: 'houseware'})
{ "_id" : ObjectId("5425a9080b73fd80bdd0724f"), "item" : "IJK2", "details" : { "model" : "14Q2", "manufacturer" : "M5 Corporation" }, "stock" : [ { "size" : "S", "qty" : 5 }, { "size" : "L", "qty" : 1 } ], "category" : "houseware" }

Specify Conditions Using Query Operators

To find documents in the inventory collection where the value of category field is either 'houseware' or 'clothing'.

> db.inventory.find({category: {$in: ['houseware', 'clothing']}})
{ "_id" : ObjectId("5425a7730b73fd80bdd0724c"), "item" : "ABC1", "details" : { "model" : "14Q3", "manufacturer" : "XYZ Company" }, "stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ], "category" : "clothing" }
{ "_id" : ObjectId("5425a9080b73fd80bdd0724d"), "item" : "ABC2", "details" : { "model" : "14Q3", "manufacturer" : "M1 Corporation" }, "stock" : [ { "size" : "M", "qty" : 50 } ], "category" : "clothing" }
{ "_id" : ObjectId("5425a9080b73fd80bdd0724e"), "item" : "MNO2", "details" : { "model" : "14Q3", "manufacturer" : "ABC Company" }, "stock" : [ { "size" : "S", "qty" : 5 }, { "size" : "M", "qty" : 5 }, { "size" : "L", "qty" : 1 } ], "category" : "clothing" }
{ "_id" : ObjectId("5425a9080b73fd80bdd0724f"), "item" : "IJK2", "details" : { "model" : "14Q2", "manufacturer" : "M5 Corporation" }, "stock" : [ { "size" : "S", "qty" : 5 }, { "size" : "L", "qty" : 1 } ], "category" : "houseware" }

You can also use $or operator to achieve the same result :

> db.inventory.find({ $or: [{ category: 'houseware'}, {category: 'clothing'}]})
{ "_id" : ObjectId("5425a7730b73fd80bdd0724c"), "item" : "ABC1", "details" : { "model" : "14Q3", "manufacturer" : "XYZ Company" }, "stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ], "category" : "clothing" }
{ "_id" : ObjectId("5425a9080b73fd80bdd0724d"), "item" : "ABC2", "details" : { "model" : "14Q3", "manufacturer" : "M1 Corporation" }, "stock" : [ { "size" : "M", "qty" : 50 } ], "category" : "clothing" }
{ "_id" : ObjectId("5425a9080b73fd80bdd0724e"), "item" : "MNO2", "details" : { "model" : "14Q3", "manufacturer" : "ABC Company" }, "stock" : [ { "size" : "S", "qty" : 5 }, { "size" : "M", "qty" : 5 }, { "size" : "L", "qty" : 1 } ], "category" : "clothing" }
{ "_id" : ObjectId("5425a9080b73fd80bdd0724f"), "item" : "IJK2", "details" : { "model" : "14Q2", "manufacturer" : "M5 Corporation" }, "stock" : [ { "size" : "S", "qty" : 5 }, { "size" : "L", "qty" : 1 } ], "category" : "houseware" }

The $or operator is not preferred when performing equality checks on the same field.

Specify AND Conditions

> db.inventory.find({ category: 'houseware', item: 'IJK2'} )
{ "_id" : ObjectId("5425a9080b73fd80bdd0724f"), "item" : "IJK2", "details" : { "model" : "14Q2", "manufacturer" : "M5 Corporation" }, "stock" : [ { "size" : "S", "qty" : 5 }, { "size" : "L", "qty" : 1 } ], "category" : "houseware" }

This query selects all documents where category is houseware and the item value is IKJ2.

Specify OR Conditions

> db.inventory.find(    {      $or: [ { qty: { $gt: 1 } }, { category: 'houseware' } ]    } )
{ "_id" : ObjectId("5425a9080b73fd80bdd0724f"), "item" : "IJK2", "details" : { "model" : "14Q2", "manufacturer" : "M5 Corporation" }, "stock" : [ { "size" : "S", "qty" : 5 }, { "size" : "L", "qty" : 1 } ], "category" : "houseware" }

References


  1. MongoDB Map Reduce
  2. MongoDB Ruby Driver Tutorial


Related Articles


Create your own user feedback survey