In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
After the last article talked about the addition, deletion and modification of mongodb, this article will explain its powerful query function.
1. Brief introduction and preliminary study of find command
The query in mongodb uses the find () command. He has 2 parameters, the first parameter is your query condition (default {} means query all), and the second parameter is to specify the key you want to return (default {} means return all). We will explain the find () command in detail below:
Initial data: db.user.save ({name: "zhangsan", age:26,sex: "women", num: [1mem2, height:164})
Db.user.save ({name: "lisi", age:22,sex: "man", num: [2mai 3je 4], height: null})
Db.user.save ({name: "wangwu", age:29,sex: "man"})
a. Query all the data in the user collection: db.user.find (). In fact, db.user.find () is an abbreviation for db.user.find ({}). It looks convenient and comfortable.
b. Query documents with name of lisi and age of 22: db.user.find ({name: "lisi", age:22})
c. From the return key specified in the returned result, the query name is lisi and only the age key is returned: db.user.find ({name: "lisi"}, {age:1}), but you will find that the "_ id" key always returns, even if you do not specify it, you can use {"_ id": 0} to prevent it from returning, such as db.user.find ({name: "lisi"}, {age:1, "_ id": 0}).
d. Specify that you do not want to have certain keys in the returned result. For example, if the query name is lisi, it does not return the key age: db.user.find ({name: "lisi"}, {age:0}), which is actually similar to the previous one, but on the contrary.
What I have to say here is the findOne command: he only returns the first piece of data in the result, such as:
Db.user.findOne ()
Second, in-depth understanding of the various queries in find ()
$lt,$lte,$gt,$gte (conditional clause): they correspond to: =.
Example: query data between 22 and 26 years old (inclusive): db.user.find ({age: {$gte:22,$lte:26}})
$ne:ne means not equal, but to put it bluntly, it doesn't mean.
Example: query data whose age is not equal to 22: db.user.find ({age: {$ne:22}})
Original address: http://1145004.blog.51cto.com/1135004/1265522
In,$all,$nin (three followed by an array): these three query operations can accept a list of one or more values, that is, an array.
In: similar to the in keyword of mysql and equivalent to the meaning of the or keyword in mysql, it means that one of the values in this array can be queried.
Example: query data with name of zhangsan and lisi: db.user.find ({name: {$in: ["zhangsan", "lisi"]}})
Nin: this is the abbreviation for not in, as opposed to in, that is, values that are not in the array will be queried.
Example: db.user.find ({name: {$nin: ["zhangsan", "wangwu"]}}) will return documents with name of lisi, but not documents with name of zhangsan and wangwu.
$all: this is very similar to $in, but $all requires that all values in the array be satisfied.
Example: db.user.find ({num: {$all: [1 all 2]}}) can find the documents of {num: [1 all 2]}, but can not find the documents of {num: [2 record4]}.
$or: because $in and so on can only query a single key, while $or is similar to or in mysql, multiple keys can be queried or queried (as long as one of them is satisfied), and or is followed by an array.
Example: query data with name of "lisi" or age of 24: db.user.find ({$or: [{name: "lis"}, {age:24}]})
$mod: indicates the balance.
For example: db.user.find ({age: {$mod: [5jue 1]}}) indicates that the age will be queried for users such as 1meme 6, 11, 16, 26, and so on. Here, users with an age of 26 will be found.
Note (heavy):
one. Conditionals (such as $lt) are the keys of the inner document, and modifiers (such as $set) are the keys of the outer document. A key can have multiple conditions, but a key cannot correspond to multiple update modifiers. For example: db.user.update ({name: "wangwu"}, {$inc: {age:1}, $set: {age:20}}) this update is not successful because the age key has been modified twice.
two. $in,$all will use the index, but $nin will not use the index, so $in,$all is recommended as a priority.
Query for null values: null is a strange thing. Let's look at it through examples.
Example: if we want to query the data whose heigth is null, db.user.find ({height:null}) you will find that not only the value of {height:null} is returned, but also the document without height key is returned. If you only want to match those with a key of null, you also need to use the $exists operator. For example, db.user.find ({height: {$in: [null], $exists:true}}) can only return documents whose height is null. Note: since mongodb does not have a "$eq" operation symbol, use $in instead.
Regular expression: the ability to match strings flexibly, such as implementing the like function in mysql.
Example:
Query name data that starts with w:
Db.user.find ({name:/ ^ w /})
Query data for name, including si:
Db.user.find ({name:/si/})
Query name includes data for si or SI (that is, case-insensitive):
Db.user.find ({name:/si/i}), I means case insensitive.
3. Array:
The previous $in,$all is for arrays, so I won't explain it here.
$size: for arrays, it can be used to query the length of a specified array.
Example: I want to query the data whose length of num is 3, db.user.find ({num: {$size:3}})
$slice: because the second argument to find is the specified returned key, but $slice returns a subset of the array. Let's use an example to illustrate its usage and function.
Example:
a. I want to return the first two values of the num array in which name is "zhangsan":
Db.user.find ({name: "zhangsan"}, {num: {$slice:2}}), all keys of rsult will be returned and the num array will only return the first two elements [1mem2].
b. I want to return the last two values of the num array where name is "zhangsan":
Db.user.find ({name: "zhangsan"}, {num: {$slice:-2}}), all keys of rsult will be returned and the num array will only return the first two elements [2mem4].
c. I want to return the second to third elements of the num array in which name is "zhangsan":
Db.user.find ({name: "zhangsan"}, {num: {$slice: [1Magazine 3]}}), all keys of rsult are returned and the num array skips the first element and returns the second and third elements [2jin4].
Query by specifying the array location:
Example: db.user.find ({"num.1": 3}) indicates that a document with a value of 3 in the second position of the num array is queried, that is, a document with a name of lisi is returned.
Count the length (size) of an array of a document:
Example: we want to get that name is the length of num group in zhangsan document, db.user.findOne ({name: "zhangsan"}). Num.length.
Query embedded documents:
Here are two pieces of initial data: db.user.save ({name: "zangsan", age:22,stu: {name: "lidao", age:21}})
Db.user.save ({name: "lisi", age:23,stu: {name: "xiaodao", age:20}})
We want to query the document of lisi student whose name is xiaodao: db.user.find ({"stu.name": "xiaodao"})
Note: we must add "" to the key when using an embedded query, otherwise it will report an error, such as "stu.name" is correct, and stu.name will report an error.
4. Cursor:
Mongodb uses cursors to return the execution results of find, because the cursor class also implements an iterator interface, so it can also be used in foreach.
Here is a simple example to illustrate:
Var result = db.user.find ()
While (result.hasNext) {
Obj = result.next ()
Print (obj.name)
}
$where: when some requirements cannot be met, $where appears and can be used to execute any js as part of a query, with which he can do almost anything.
Example: query data over the age of 26:
Db.user.find ({$where: "this.age > 26"})
He is equivalent to: = > var f = function () {return this.age > 26;}
= > db.user.find (f)
$limit: limits the use of returned results. In addition, $limit specifies the upper limit instead of the lower limit, and returns all of the query if it is not enough.
Example: I want to return the first two items of all arrays in the user collection, db.user.find (). Limit (2)
$skip: it works with $limit and can also be used alone, and try to avoid too many results. Let's illustrate it with examples.
Example: db.user.find (). Skip (1); returns from the second piece of data, that is, only documents with name of lisi and wangwu are returned.
Db.user.find () .skip (1) .limit (1); indicates that it returns from the second piece of data and only the first piece of data is returned, that is, the document whose name is lisi.
Note: $skip is not suitable for paging, because if you have 1 million data, you have to skip a large number of results, so the efficiency is very you, not suitable for paging.
$sort: sorts the returned results.
Example: db.user.find (). Sort ({age:1}), which means that the returned results are sorted in ascending order by the age key.
The last one describes the distinct operation: represents the simplest tool to get a list of different values in a particular key.
Example: db.user.find ("name") he will return ["zhangsan", "lisi", "wangwu"]
Note: distinct returns are limited, and his return result must be 16m.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.