How to query data in MongoDB
MongoDB how to query data, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Before we begin, we should prepare the data to facilitate the demonstration. Here I have inserted a few pieces of data, as follows:
Db.user.insertMany ([{name:'jack', age:22, sex:'Man', tags: ['python','c++','c'], grades: [22pr 3je 44tags 55], school: {name:'shida', city:'xuzhou'}}, {name:'jhon', age:33, sex:null, tags: [' python','java'], grades: [6pr 22pr 4cr 88], school: {name:'kuangda', city:'xuzhou'}}, {name:'xiaoming', age:33 Tags: ['python','java'], grades: [6, 22, 44, 88], school: {name:'kuangda', city:'xuzhou'}}])
Find (,)
Where query represents the search condition, which is equivalent to the where clause in mysql. Projection lists the data you want to find in the format of db.collection.find (find (,)).
Example:
The following lookup without parameters will find all the results
Db.find () .pretty () / / output result {"_ id": ObjectId ("59056f81299fe049404b2899"), "name": "jack", "age": 22 "tags": ["python", "C++" "c"], "grades": [22 33, 44, 55] "school": {"name": "shida" "city": "xuzhou"}}
Next, find the data that satisfies that name is jack, and output only name, age. The _ id here is output by default. If you don't want to export, set it to 0. If you want to output that field, set it to 1.
Db.user.find ({name:'jack'}, {name:1,age:1}) / / output result {"_ id": ObjectId ("59056f81299fe049404b2899"), "name": "jack", "age": 22} db.user.find ({name:'jack'}, {name:1,age:1,_id:0}) / / output result {"name": "jack", "age": 22}
* * Note that a projection cannot specify both include and exclude fields, except for excluding the _ id field. In mappings that explicitly include fields, the _ id field is * one that you can explicitly exclude.
Query embedded documents
The school data inserted in the above example represents an embedded document
Exact match query
An exact match query means that the query array in school must be exactly the same as the inserted array, and the order must be the same before it can be found.
Db.user.find ({name:'jack',school: {name:'shida',city:'xuzhou'}}) / / output result {"_ id": ObjectId ("59056f81299fe049404b2899"), "name": "jack", "age": 22, "tags": ["python", "C++", "c"], "grades": [22, 33, 44, 55], "school": {"name": "shida", "city": "xuzhou"} / / below are the fields specified for output The school.name here means that only the name field in the school document is output, and you must quote db.user.find ({name:'jack',school: {name:'shida',city:'xuzhou'}}, {name:1,age:1,'school.name':1}). / / output result {"_ id": ObjectId ("59056f81299fe049404b2899"), "name": "jack", "age": 22, "school": {"name": "shida"}}
Key-value pair query
You can query through key-value pairs, regardless of the order. For example, 'school.name':'shida'' means to query the data whose school name is shida. The quotation marks here are required.
Db.user.find ({'school.name':'shida'}, {name:1,school:1}); / output result {"_ id": ObjectId ("59056f81299fe049404b2899"), "name": "jack", "school": {"name": "shida", "city": "xuzhou"}}
Query operator
Next we will cooperate with the query operator to perform complex query operations, such as element query, logical query, and compare query operations. We use the following comparison operators "$gt", "$gte", "$lt", "$lte" (corresponding to ">", "> =", "