Top Banner
MongoDB Kim 2015.05.01
28
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MongoDB

MongoDBKim 2015.05.01

Page 2: MongoDB

Document

{

"_id": "10280",

"city": "NEW YORK",

"state": "NY",

"pop": 5574,

"loc": [-74.016323, 40.710537]

}

Page 3: MongoDB

Query Documents

db.collection.find(query, projection)

Page 4: MongoDB

Query Documents

Select All Documents in a Collection

db.inventory.find( {} )

db.inventory.find()

Page 5: MongoDB

Query Documents

Specify Equality Condition

To specify equality condition, use the query document { <field>: <value> } to select all documents that contain the

<field> with the specified <value>.

db.inventory.find( { type: "snacks" } )

Page 6: MongoDB

Query Documents

Specify Conditions Using Query Operators

The following example selects all documents in the inventory collection where the value of the typefield is either

'food' or 'snacks'

db.inventory.find( { type: { $in: [ 'food', 'snacks' ] } } )

Although you can express this query using the $or operator, use the $in operator rather than the $oroperator when

performing equality checks on the same field.

Page 7: MongoDB

Query Documents

Specify AND Conditions

db.inventory.find( { type: 'food', price: { $lt: 9.95 } } )

This query selects all documents where the type field has the value 'food' and the value of the pricefield is less than

9.95. See comparison operators for other comparison operators.

Page 8: MongoDB

Query Documents

Specify OR Conditions

db.inventory.find(

{

$or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ]

}

)

Page 9: MongoDB

Query Documents

Embedded Documents

In the following example, the query matches all documents where the value of the field producer is an embedded

document that contains only the field company with the value 'ABC123' and the field address with the value '123 Street', in the exact order:

db.inventory.find({

producer: {

company: 'ABC123',

address: '123 Street'

}

})

Page 10: MongoDB

Query Documents

Equality Match on Fields within an Embedded Document

In the following example, the query uses the dot notation to match all documents where the value of the field produceris an embedded document that contains a field company with the value 'ABC123' and may contain other fields:

db.inventory.find( { 'producer.company': 'ABC123' } )

Page 11: MongoDB

Query Documents

{ _id: 5, type: "food", item: "aaa", ratings: [ 5, 8, 9 ] }

{ _id: 6, type: "food", item: "bbb", ratings: [ 5, 9 ] }

{ _id: 7, type: "food", item: "ccc", ratings: [ 9, 5, 8 ] }

The following example queries for all documents where the field ratings is an array that holds exactly three elements,

5, 8, and 9, in this order:

db.inventory.find( { ratings: [ 5, 8, 9 ] } )

The operation returns the following document:

{ "_id" : 5, "type" : "food", "item" : "aaa", "ratings" : [ 5, 8, 9 ] }

Page 12: MongoDB

Query Documents

Match an Array Element

db.inventory.find( { ratings: 5 } )

The operation returns the following documents:

{ "_id" : 5, "type" : "food", "item" : "aaa", "ratings" : [ 5, 8, 9 ] }

{ "_id" : 6, "type" : "food", "item" : "bbb", "ratings" : [ 5, 9 ] }

{ "_id" : 7, "type" : "food", "item" : "ccc", "ratings" : [ 9, 5, 8 ] }

Page 13: MongoDB

Query Documents

Match a Specific Element of an Array

In the following example, the query uses the dot notation to match all documents where the ratings array contains 5 as

the first element:

db.inventory.find( { 'ratings.0': 5 } )

The operation returns the following documents:

{ "_id" : 5, "type" : "food", "item" : "aaa", "ratings" : [ 5, 8, 9 ] }

{ "_id" : 6, "type" : "food", "item" : "bbb", "ratings" : [ 5, 9 ] }

Page 14: MongoDB

Insert Documents

db.inventory.insert(

{

item: "ABC1",

details: {

model: "14Q3",

manufacturer: "XYZ Company"

},

stock: [ { size: "S", qty: 25 }, { size: "M", qty: 50 } ],

category: "clothing"

}

)

Page 15: MongoDB

Insert Documents

var mydocuments =

[

{

item: "ABC2",

details: { model: "14Q3", manufacturer: "M1 Corporation" }

},

{

item: "MNO2",

stock: [ { size: "S", qty: 5 }, { size: "M", qty: 5 }, { size: "L", qty: 1 } ]

},

];

db.inventory.insert( mydocuments );

Page 16: MongoDB

Insert Documents

Insert Multiple Documents with Bulk

Initialize a Bulk operations builder for the collection inventory.

var bulk = db.inventory.initializeUnorderedBulkOp();

Page 17: MongoDB

Insert Documents

bulk.insert(

{

item: "BE10",

details: { model: "14Q2", manufacturer: "XYZ Company" },

stock: [ { size: "L", qty: 5 } ],

}

);

bulk.insert(

{

item: "ZYT1",

details: { model: "14Q1", manufacturer: "ABC Company" },

stock: [ { size: "S", qty: 5 }, { size: "M", qty: 5 } ],

}

);

bulk.execute();

Page 18: MongoDB

Modify Documents

db.collection.update(query, update, options)

db.collection.update(

<query>,

<update>,

{

upsert: <boolean>,

multi: <boolean>

}

)

Page 19: MongoDB

Modify Documents

Update Specific Fields in a Document

db.inventory.update(

{ item: "MNO2" },

{

$set: {

category: "apparel",

details: { model: "14Q3", manufacturer: "XYZ Company" }

},

$currentDate: { lastModified: true }

}

)

Page 20: MongoDB

Modify Documents

Update an embedded field

db.inventory.update(

{ item: "ABC1" },

{ $set: { "details.model": "14Q2" } }

)

Page 21: MongoDB

Modify Documents

Update multiple documents

db.inventory.update(

{ category: "clothing" },

{

$set: { category: "apparel" },

$currentDate: { lastModified: true }

},

{ multi: true }

)

Page 22: MongoDB

Modify Documents

Specify upsert: true for the update replacement operation

db.inventory.update(

{ item: "TBD1" },

{

item: "TBD1",

details: { "model" : "14Q4", "manufacturer" : "ABC Company" },

stock: [ { "size" : "S", "qty" : 25 } ],

category: "houseware"

},

{ upsert: true }

)

Page 23: MongoDB

Modify Documents

Specify upsert: true for the update specific fields operation.

db.inventory.update(

{ item: "TBD2" },

{

$set: {

details: { "model" : "14Q3", "manufacturer" : "IJK Co." },

category: "houseware"

}

},

{ upsert: true }

)

Page 24: MongoDB

Remove Documents

db.collection.remove(

<query>,

<justOne>

)

Page 25: MongoDB

Remove Documents

The following example removes all documents from the inventory collection:

db.inventory.remove({})

Page 26: MongoDB

Remove Documents

Remove Documents that Match a Condition

db.inventory.remove( { type : "food" } )

Page 27: MongoDB

Iterate a Cursor in the mongo Shell

The db.collection.find() method returns a cursor. To access the documents, you need to iterate the cursor.

var myCursor = db.inventory.find( { type: 'food' } );

myCursor.forEach(printjson);

Page 28: MongoDB

Iterate a Cursor in the mongo Shell

● cursor.count() Returns the total number of documents in a cursor.

● cursor.forEach() Applies a JavaScript function for every document in a cursor.

● cursor.hasNext() Returns true if the cursor has documents and can be iterated.

● cursor.limit() Constrains the size of a cursor’s result set.

● cursor.sort() Returns results ordered according to a sort specification.

● cursor.toArray() Returns an array that contains all documents returned by the cursor.