I have described the addition of MongoDB, deletion and modification of the basic operation, here are some of the methods of the query.
Find
The most basic is the find and FindOne method. Find returns all the documents in the collection, and if you want to view only one document, you can use FindOne. When you use Find, the shell automatically displays a maximum of 20 matching documents.
MongoDB uses find to query. The query is to return a subset of the document in a collection, ranging from 0 documents to the entire collection.
the first parameter of find determines which documents to return . The form is also a document that describes the details to be queried. The empty query document {} will match the entire contents of the collection. If you do not specify a query document, the default is {}. such as: Db.person.find () returns all the contents of the collection person. Adding a key-value pair to a query document means that the query criteria are added. For most types, integers match integers, Boolean types match Boolean types, and string match strings.
returns the specified item
Sometimes we don't need to return all the key values in the document, so we can specify the key to return by using the second parameter of Find or FindOne. This saves both the amount of data transferred and the time and memory consumption of the client decoding the document.
In the following example, only the Id,age,sex property is returned:
You can also use the second parameter to reject a key-value pair in the query result, for example:
The key name does not appear in the returned results:
db.person.findOne({"name":"lf"},{"name":0})
Only the age and sex are returned, and the ID does not return:
db.person.findOne({"name":"lf"},{"age":1,"sex":1,"_id":0})
Query Criteria
MongoDB's syntax is not like SQL, and its conditional judgments are all implemented by code rather than symbols.
mongodb |
sql |
$lt |
< |
|
<= |
|
> |
|
>= |
|
!= |
|
between |
|
not between |
|
|
|
not |
|
|
|
|
$GT###
Such as:
Inquiry Age >=18 <=30:
Here's how:
db.person.find({"age":{"$gte":18,"$lte":30}})
Another example of the date of the query, the first to add a key to the document birthday,
db.person.update( {"name":"lf"}, { "$set": { "birthday":newDate("1992/7/22") } })
Query birthday date is after 1990-1-1 person:
db.person.find({"birthday":{"$gt":newDate("1990/01/01")}})
$ne
Find all documents with name not equal to LF, where the document with name does not exist in the collection will also be detected:
db.person.find({"name":{"$ne":"lf"}})
$in
$in can be used to query for values in the range, for example: Querying for data with pageviews as 10000,20000:
db.person.find({pageViews:{"$in":[10000,20000]}})
$in can also be used to specify different types of conditions and values, such as the process of migrating a user's ID number into a user name, to do both of the queries:
db.person.find({"user_id":{"$in":[12345,"lf"]}})
This will match the documentation for USER_ID 12345 and LF.
If there is only one value in $in, it is the same as the effect of directly matching this value:
db.person.find({"pageViews":{"$in":[10000]}})db.person.find({"pageViews":10000})
$nin
$nin can return documents that do not match all the conditions in the array.
such as: Find all pageviews unequal 10000,20000 documents, note that the document does not exist in the key pageviews document will be detected:
db.person.find({"pageViews":{"$nin":[10000,20000]}})
$or
$or is used to return or result, as long as any one of the conditions array is satisfied, it will return:
db.person.find( { "$or": [ {"pageViews":{"$in":[10000,20000]}}, {"url":"http://blog.cdsn.net/mevicky "} ] })
The document that will query out pageviews is 10000,20000 or the URL is http://blog.cdsn.net/mevicky.
$not
$not can be used on top of any condition:
db.users.find( {"id_num":{"$not":{"mod":[1,5]}}})
$size
$size can be used to query an array of the specified length:
db.person.find({"emails":{"$size":2}})
One thing to note here is that the $size method cannot be used in conjunction with other query clauses.
such as $GT, so we can use another way to define a size key for each array document,
Add the original array to the operation:
db.person.update({"$push":{"emails":"362512489@qq.com"}})
Switch
db.person.update({"$push":{"emails":"362512489@qq.com "},"$inc":{"size":1}})
This makes it possible to query:
db.person.find({"size":{"$gt":3}})
$slice
$slice used to return a subset of the array,
For example:
Returns the first two elements of a emails array
db.person.find({"userName":"lf"},{"emails":{"$slice":2}})
Returns the latter two elements of the emails array
db.person.find({"userName":"lf"},{"emails":{"$slice":-2}})
Returns the 2nd and 11th elements of the emails array. If the array is not 11, return all elements after 2nd
db.person.find({"userName":"lf"},{"emails":{"$slice":[1,10]}})
Regular Queries
Like relational databases, MongoDB supports regular expression queries, allowing for flexible and efficient matching of strings.
MongoDB query is not lost in the relational database, the query conditions of the complex usage is far from all covered, want to master the comprehensive and thorough further study.
Inline Document Query
The following is a query of the embedded document, it is well known that the working mode of NoSQL storage documents represents that the data model can be very complex and flexible, then it will be difficult to query it?
There are two ways to query an inline document: Query the entire document , or query only for its key-value pairs .
Querying the entire inline document is the same way as a normal query:
db.person.insert( { "name": { "first":"lf", "last":"tyq" }, "age":23 })
Find a document named name
db.person.find({"name":{"first":"lf","last":"tyq"}})
But
db.person.find({"name":{"first":"lf"}})
This does not make it possible to query the document.
Therefore, it is best to search through the specific key values of the embedded document. This will not cause the query to fail even if the data model changes:
db.person.find({"name.first":"lf","name.last":"tyq"})db.person.find({"name.first":"lf"})//这个也可以查出文档.
Is it convenient to use the. operator to embed the document in step steps? When the document structure becomes more complex, the matching of the inline document requires more skill:
db.person.insert( { "title":"blog", "comments": [ { "author":"lf", "score":3, "comment":"nice" }, { "author":"tyq", "score":6, "comment":"good" } ] })
Now we want to query the key author for a comment of tyq,score greater than or equal to 5, and cannot use:
db.person.find( {"comments":{"author":"tyq","score":{"$gte":5}}})
The point operator also does not get the result:
db.person.find( {"comments.author":"tyq","comments.score":{"$gte":5}})
What is this for? Because comments that meet author conditions and score-compliant comments may not be the same comment .
Therefore, it is necessary to use $elemmatch to determine a set of conditions without specifying each key:
db.person.find( { "comments": { "$elemMatch": { "author":"tyq", "score":{"$gte":5} } } })
$elemMatch to group the qualifying conditions only if you need multiple key operations on an inline document .
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
MongoDB Increase and deletion check (ii)