MongoDB Operations Manual CRUD queries

Source: Internet
Author: User
Tags mongodb documentation

Query OperationsBasic Queryquerying all records in a specified collection db.testData.find () or db.testData.find ({});
Equality criteria queryDb.testData.find ( {num:5}); Querying the records of num=5
declaring multiple conditions using the query operator Db.testData.find ({num:{$in: [2,3,4]}}); Query the records for NUM as 2,3,4.
Although you can use the $or operator to perform the same query, use $in instead of $or when multiple or conditions are equal judgments on the same field.
and condition queryDb.testData.find ( {name: ' d ', num:{$lt: Ten}}); Query for records with name D and Num less than 10. lt = Less Than
or condition queryDb.find ( {$or: [{name: ' A '},{num:{$GT: 3}])//query for records with name A or num greater than 3. GT = Greater Than
And or mixed query

Db.testData.find ({gender: ' Male ', $or: [{name: ' A '},{age:{$GT: 3}]}); Query gender to Male, and (name A or age>3) records.


Embedded Record QueryWhen a field is embedded with a record, the query can either declare the exact (i.e., field order, field values match) to match some fields in the inline record, or use "." To match the individual field declarations for the inline record.
test DataDb.testData.insert ({producer:{company: ' A ', Address: ' 123 '},name: ' AAA '});
Db.testData.insert ({producer:{company: ' A ', Address: ' 123 '}});
a field that exactly matches an inline record In the original Db.testData.find (<field>:<value>), change <value> to a matching inline record
Example:db.testData.find ({producer:{company: ' A ', Address: ' 123 '}});
Results will return the above two test data
Attention:
Db.testData.find ({producer:{address: ' 123 ', Company: ' A '}})
Db.testData.find ({producer:{address: ' 123 ', Name: ' AAA '}})
Db.testData.find ({producer:{company: ' A ', Name: ' AAA '}})
None of these three statements will query the result, which means that the query must match from the first to Nth fields, the field order is reversed, or the results will not be found next to each other.
Match some inline fields Db.testData.find ({' Producer.company ': ' A '});
Db.testData.find ({' Producer.company ': ' A ', ' producer.address ': ' 123 '});
The above two can query all the test data.
Attention:
Db.testData.find ({' Producer.company ': ' A ', ' producer.name ': ' AAA '});
Db.testData.find ({' producer.address ': ' 123 ', ' producer.name ': ' AAA '});
Db.testData.find ({' Producer.name ': ' AAA '});
These three statements are also unable to query the test data, which means that the method must match the first to Nth fields, the order of the fields, or the fields that are not next to each other will not be found.

Array OverviewWhen a field holds an array, you can query the record based on a value in the array. If this array holds an embedded record, you can use the "." Inquire.
If you use $elemmatch to declare multiple criteria for querying, the array must contain at least one record that satisfies all conditions.
If you do not use $elemmatch to declare multiple criteria for querying, then the criteria that satisfy the condition is that the individual elements in this array can override each requirement of the condition, such as the set of 5,6,7, if the condition is =5 and =7, The combination of 5 and 72 elements in the array can cover all the conditional requirements.

test DataDb.testData.insert ({_id:5, type: "Food", Item: "AAA", ratings: [5, 8, 9]});
Db.testData.insert ({_id:6, type: "Food", Item: "BBB", ratings: [5, 9]});
Db.testData.insert ({_id:7, type: "Food", Item: "CCC", ratings: [9, 5, 8]});

exact match arraythe query results need to match each value in the array exactly, including the order of each element Db.testData.find ( {ratings: [5, 8, 9]})
match one of the values in the array Db.testData.find ({ratings:5})
matches the value of the specified position in the array Db.testData.find ( {' ratings.0 ': 5})//matches the record of the first element in the ratings array to 5
Multi-conditional query for arrays Single element conditional query Db.testData.find ( {ratings: {$elemMatch: {$gt: 5, $lt: 9}})//matches at least one element in the ratings array is greater than 5 and less than 9 of the records.
Multi-Element Union matching query Db.testData.find ({ratings: {$gt: 5, $lt: 9}})//Match array has one element greater than 5, a record with another element less than 9, or an element that satisfies both conditions.

Inline Record Array test Data{
_ID:100,
Type: "Food",
Item: "XYZ",
QTY:25,
3.3. MongoDB CRUD Tutorials 91
MongoDB documentation, Release 2.6.4
price:2.5,
Ratings: [5, 8, 9],
Memos: [{memo: ' On Time ', by: ' Shipping '}, {memo: ' Approved ', by: ' Billing '}]
}
{
_ID:101,
Type: "Fruit",
Item: "JKL",
Qty:10,
price:4.25,
Ratings: [5, 9],
Memos: [{memo: ' On Time ', by: ' Payment '}, {memo: ' Delayed ', by: ' Shipping '}]
}
use array subscripts to match a field in an embedded record If you know the array order of the embedded records, you can use the following query
db.testData.find ({' memos.0.by ': ' Shipping '})//Match record of the first element in the memos array by field shipping
array subscript Matching fields are not required If you do not know the array order, use the following query
db.testData.find ({' memos.by ': ' Shipping '})//Match a record in the memos array containing the by field with a value of shipping

Multi-conditional query for arrays as with array queries, using $elemmatch is to have an element in the arrays satisfy all the conditions, not to say that multiple elements are combined to satisfy all conditions
Single element conditional query Use $elemmatch to query an element in an array to satisfy all records
Db.testData.find (
{
Memos:
{
$elemMatch:
{
Memo: ' On Time ',
By: ' Shipping '
}
}
}
//Match an element in memos with a record of memo as ' on time ' and by ' shipping '
combination satisfies a condition query Db.inventory.find (
{
' Memos.memo ': ' On Time ',
' memos.by ': ' Shipping '
}
) //Match an element in the memos array that contains a Memo field and the value is on time, while another element contains a record with the by field and a value of shipping, or an element satisfies two records at the same time

MongoDB Operations Manual CRUD queries

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.