摘自MongoDB Manual 3.6,都是重点,本想翻译成中文,无奈确实有点忙…
##Insert Documents
insertOne(), insertMany() returns a document that includes the newly inserted documents _id field values.
####Behavior
If the collection does not currently exist, insert operations will create the collection.
In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.
This also applies to documents inserted through update operations with upsert: true.
All write operations in MongoDB are atomic on the level of a single document.
##Query Documents
####Select All Documents in a Collection
db.inventory.find( {} )
SELECT * FROM inventory
####Specify Equality Condition
{ : , ... }
db.inventory.find( { status: "D" } )
SELECT * FROM inventory WHERE status = "D"
####Specify Conditions Using Query Operators
{ : { : }, ... }
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
SELECT * FROM inventory WHERE status in ("A", "D")
NOTE: Although you can express this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.
####Specify AND Conditions
A compound query can specify conditions for more than one field in the collection’s documents. Implicitly, a logical AND conjunction connects the clauses of a compound query so that the query selects the documents in the collection that match all the conditions.
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
SELECT * FROM inventory WHERE status = "A" AND qty < 30
####Specify OR Conditions
Using the $or operator, you can specify a compound query that joins each clause with a logical OR conjunction so that the query selects the documents in the collection that match at least one condition.
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
SELECT * FROM inventory WHERE status = "A" OR qty < 30
####Specify AND as well as OR Conditions
db.inventory.find( {
status: "A",
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )
SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
NOTE: MongoDB supports regular expressions $regex queries to perform string pattern matches.
##Query on Embedded/Nested Documents
####Match an Embedded/Nested Document
To specify an equality condition on a field that is an embedded/nested document, use the query filter document { : } where is the document to match.
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
NOTE: Equality matches on the whole embedded document require an exact match of the specified document, including the field order.
####Query on Nested Field
To specify a query condition on fields in an embedded/nested document, use dot notation (“field.nestedField”).
NOTE: When querying using dot notation, the field and nested field must be inside quotation marks.
######Specify Equality Match on a Nested Field
db.inventory.find( { "size.uom": "in" } )
######Specify Match using Query Operator
{ : { : }, ... }
db.inventory.find( { "size.h": { $lt: 15 } } )
######Specify AND Condition
db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )
##Query an Array
####Match an Array
To specify equality condition on an array, use the query document { : } where is the exact array to match, including the order of the elements.
db.inventory.find( { tags: ["red", "blank"] } )
If, instead, you wish to find an array that contains both the elements “red” and “blank”, without regard to order or other elements in the array, use the $all operator:
db.inventory.find( { tags: { $all: ["red", "blank"] } } )
####Query an Array for an Element
To query if the array field contains at least one element with the specified value, use the filter { : } where is the element value.
db.inventory.find( { tags: "red" } )
To specify conditions on the elements in the array field
{ : { : , ... } }
db.inventory.find( { dim_cm: { $gt: 25 } } )
####Specify Multiple Conditions for Array Elements
When specifying compound conditions on array elements, you can specify the query such that either a single array element meets these condition or any combination of array elements meets the conditions.
######Query an Array with Compound Filter Conditions on the Array Elements
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )
######Query for an Array Element that Meets Multiple Criteria
Use $elemMatch operator to specify multiple criteria on the elements of an array such that at least one array element satisfies all the specified criteria.
db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
######Query for an Element by the Array Index Position
Using dot notation, you can specify query conditions for an element at a particular index or position of the array. The array uses zero-based indexing.
db.inventory.find( { "dim_cm.1": { $gt: 25 } } )
######Query an Array by Array Length
Use the $size operator to query for arrays by number of elements.
db.inventory.find( { "tags": { $size: 3 } } )
##Query an Array of Embedded Documents
####Query for a Document Nested in an Array
db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )
NOTE: Equality matches on the whole embedded/nested document require an exact match of the specified document, including the field order.
####Specify a Query Condition on a Field in an Array of Documents
######Specify a Query Condition on a Field Embedded in an Array of Documents
If you do not know the index position of the document nested in the array, concatenate the name of the array field, with a dot (.) and the name of the field in the nested document.
db.inventory.find( { 'instock.qty': { $lte: 20 } } )
######Use the Array Index to Query for a Field in the Embedded Document
Using dot notation, you can specify query conditions for field in a document at a particular index or position of the array. The array uses zero-based indexing.
db.inventory.find( { 'instock.0.qty': { $lte: 20 } } )
NOTE: When querying using dot notation, the field and index must be inside quotation marks.
####Specify Multiple Conditions for Array of Documents
When specifying conditions on more than one field nested in an array of documents, you can specify the query such that either a single document meets these condition or any combination of documents (including a single document) in the array meets the conditions.
######A Single Nested Document Meets Multiple Query Conditions on Nested Fields
Use $elemMatch operator to specify multiple criteria on an array of embedded documents such that at least one embedded document satisfies all the specified criteria.
db.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )
db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )
######Combination of Elements Satisfies the Criteria
If the compound query conditions on an array field do not use the $elemMatch operator, the query selects those documents whose array contains any combination of elements that satisfies the conditions.
db.inventory.find( { "instock.qty": { $gt: 10, $lte: 20 } } )
db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )
##Project Fields to Return from Query
By default, queries in MongoDB return all fields in matching documents. To limit the amount of data that MongoDB sends to applications, you can include a projection document to specify or restrict fields to return.
####Return All Fields in Matching Documents
If you do not specify a projection document, the db.collection.find() method returns all fields in the matching documents.
db.inventory.find( { status: "A" } )
SELECT * from inventory WHERE status = "A"
####Return the Specified Fields and the _id Field Only
A projection can explicitly include several fields by setting the to 1 in the projection document.
db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
SELECT _id, item, status from inventory WHERE status = "A"
####Suppress _id Field
You can remove the _id field from the results by setting its exclusion to 0 in the projection
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
SELECT item, status from inventory WHERE status = "A"
####Return All But the Excluded Fields
Instead of listing the fields to return in the matching document, you can use a projection to exclude specific fields.
db.inventory.find( { status: "A" }, { status: 0, instock: 0 } )
NOTE: With the exception of the _id field, you cannot combine inclusion and exclusion statements in projection documents.
####Return Specific Fields in Embedded Documents
You can return specific fields in an embedded document. Use the dot notation to refer to the embedded field and set to 1 in the projection document.
db.inventory.find(
{ status: "A" },
{ item: 1, status: 1, "size.uom": 1 }
)
####Suppress Specific Fields in Embedded Documents
You can suppress specific fields in an embedded document. Use the dot notation to refer to the embedded field in the projection document and set to 0.
db.inventory.find(
{ status: "A" },
{ "size.uom": 0 }
)
####Projection on Embedded Documents in an Array
Use dot notation to project specific fields inside documents embedded in an array.
db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } )
####Project Specific Array Elements in the Returned Array
For fields that contain arrays, MongoDB provides the following projection operators for manipulating arrays: $elemMatch, $slice, and $.
$elemMatch, $slice, and $ are the only way to project specific elements to include in the returned array. For instance, you cannot project specific array elements using the array index; e.g. { “instock.0”: 1 } projection will not project the array with the first element.
db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )
##Query for Null or Missing Fields
Different query operators in MongoDB treat null values differently.
####Equality Filter
The { item : null } query matches documents that either contain the item field whose value is null or that do not contain the item field.
db.inventory.find( { item: null } )
####Type Check
The { item : { $type: 10 } } query matches only documents that contain the item field whose value is null
db.inventory.find( { item : { $type: 10 } } )
####Existence Check
The { item : { $exists: false } } query matches documents that do not contain the item field:
db.inventory.find( { item : { $exists: false } } )
##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. However, in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times [1] to print up to the first 20 documents in the results.
####Manually Iterate the Cursor
In the mongo shell, when you assign the cursor returned from the find() method to a variable using the var keyword, the cursor does not automatically iterate.
You can call the cursor variable in the shell to iterate up to 20 times [1] and print the matching documents
var myCursor = db.users.find( { type: 2 } );
myCursor
You can also use the cursor method next() to access the documents
var myCursor = db.users.find( { type: 2 } );
while (myCursor.hasNext()) {
print(tojson(myCursor.next()));
}
As an alternative print operation, consider the printjson() helper method to replace print(tojson())
var myCursor = db.users.find( { type: 2 } );
while (myCursor.hasNext()) {
printjson(myCursor.next());
}
You can use the cursor method forEach() to iterate the cursor and access the documents
var myCursor = db.users.find( { type: 2 } );
myCursor.forEach(printjson);
NOTE: You can use the DBQuery.shellBatchSize to change the number of iteration from the default value 20.
####Iterator Index
In the mongo shell, you can use the toArray() method to iterate the cursor and return the documents in an array, as in the following:
var myCursor = db.inventory.find( { type: 2 } );
var documentArray = myCursor.toArray();
var myDocument = documentArray[3];
The toArray() method loads into RAM all documents returned by the cursor; the toArray() method exhausts the cursor.
Additionally, some drivers provide access to the documents by using an index on the cursor (i.e. cursor[index]). This is a shortcut for first calling the toArray() method and then using an index on the resulting array.
var myCursor = db.users.find( { type: 2 } );
var myDocument = myCursor[1];
####Cursor Behaviors
####Cursor Information
##Update Documents
db.collection.updateOne(, , )
db.collection.updateMany(, , )
db.collection.replaceOne(, , )
####Update Documents in a Collection
To update a document, MongoDB provides update operators, such as $set, to modify field values.
{
: { : , ... },
: { : , ... },
...
}
######Update a Single Document
db.inventory.updateOne(
{ item: "paper" },
{
$set: { "size.uom": "cm", status: "P" },
$currentDate: { lastModified: true }
}
)
######Update Multiple Documents
db.inventory.updateMany(
{ "qty": { $lt: 50 } },
{
$set: { "size.uom": "in", status: "P" },
$currentDate: { lastModified: true }
}
)
####Replace a Document
To replace the entire content of a document except for the _id field, pass an entirely new document as the second argument to db.collection.replaceOne().
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.
The replacement document can have different fields from the original document. In the replacement document, you can omit the _id field since the _id field is immutable; however, if you do include the _id field, it must have the same value as the current value.
db.inventory.replaceOne(
{ item: "paper" },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
)
####Behavior
All write operations in MongoDB are atomic on the level of a single document.
Once set, you cannot update the value of the _id field nor can you replace an existing document with a replacement document that has a different _id field value.
When performing update operations that increase the document size beyond the allocated space for that document, the update operation relocates the document on disk.
MongoDB preserves the order of the document fields following write operations except for the following cases: The _id field is always the first field in the document. Updates that include renaming of field names may result in the reordering of fields in the document.
####Upsert Option
If updateOne(), updateMany(), or replaceOne() includes upsert : true and no documents match the specified filter, then the operation creates a new document and inserts it. If there are matching documents, then the operation modifies or replaces the matching document or documents.
##Delete Documents
####Delete All Documents
To delete all documents from a collection, pass an empty filter document {} to the db.collection.deleteMany() method.
db.inventory.deleteMany({})
####Delete All Documents that Match a Condition
You can specify criteria, or filters, that identify the documents to delete. The filters use the same syntax as read operations.
To delete all documents that match a deletion criteria, pass a filter parameter to the deleteMany() method.
{ : , ... }
{ : { : }, ... }
db.inventory.deleteMany({ status : "A" })
####Delete Only One Document that Matches a Condition
To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the db.collection.deleteOne() method.
db.inventory.deleteOne( { status: "D" } )
####Behavior
Delete operations do not drop indexes, even if deleting all documents from a collection.
All write operations in MongoDB are atomic on the level of a single document.