Insert Documents
Insert a document into a collection.
MongoDB provides a Bulk( ) API that you can use to perform multiple write operations in bulk.
- initialize the operation builder
- add insert operation
- execute
Query Documents
Select all document in a collection.
Specify equality condition
- use { <field> : <value> }
Specify conditions using query operators
Specify AND condition
Specify OR condition
Exact Match on the Embedded Document
Equality Match on Fields within an Embedded Document
Exact Match on an Array
- Match every element in the array, including the order
Match an Array Element
- The array contains at least one element with the specified value.
Match a Specify Element of an Array
- Match at a particular position in the array
Single Element Satisfies the Criteria
- Use $elemMatch to specify multiple criteria on the elements of an array such that at least one element in the array satisfies all criteria.
Combination of Elements Satisfies the Criteria
Matching a Field in the Embedded Document Using the Array Index
Match a Field Without Specifying Array Index
- Select the document where the ‘memos’ field contains an array that at least one embedded document contains the field ‘by’ with ‘shipping’.
Single Element Satisfies the Criteria
Combination of Elements Satisfies the Criteria
Modify Documents
Use update operators to change field values.
Update an embedded field.
- Use dot notation
Update multiple documents
- Use the multi option in update
Replace a document.
Specify upsert: true for the update replacement operation.
Specify uperst: true for the update specific fields operation.
- If no document matches, a new document would be inserted.
Remove Documents
Remove All Documents
Remove Documents that Match a Condition
Remove a Single Document that Matches a Condition
Limit Fields to Return from a Query
You can not combine inclusion and exclusion semantics in a single projection with the exception of _id.
Iterate a Cursor
Use toArray( ) to convert the cursor into an array. It will consume all the cursor.
Query with Index
Two Phase Commit
Create an Auto-Incrementing Sequence Field
- Use Counters Collection
- use a collection to hold the last id, use a function to get and update the id from this counter collection.
- Optimistic Loop
- calculates the incremented _id value and attempts to insert a document with the calculated _id until the insertion is successful.
没有评论:
发表评论