In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
1. General inquiry
Find method syntax:
Find ([conditions, fields to be displayed])
Note:
1. If there are no conditions, query all
2. If the specified _ id field is not shown, it will be shown by default, such as find ({}, {"_ id": 0}).
1.1. View all the documents in the collection
Command: db. Collection name. Find ()
1.2. View the first document in the collection
Command: db. Collection name .findOne ({conditional object})
1.3. Specify the key to be returned
It is specified in the second parameter of the find method. By default, "_ id" is always returned, and you can set the field to 0 to indicate that it is not returned.
2. Conditional inquiry
Find method syntax:
Find ([conditions, fields to be displayed])
You can add conditional data to the find method, and the first parameter of the find method is.
Note: conditional data must be a constant value, not data from another field
1: compare operation
$lt: less than
$lte: less than or equal to
$gt: greater than
$gte: greater than or equal to
$ne: not equal to
Such as: a, age less than 20
> db.test1.find ({"age": {$lt:20}})
B, age less than 20, greater than 10
> db.test1.find ({"age": {$lt:20,$gt:10}})
2:$and: contains multiple conditions, and the relationship between them is and
> db.test1.find ({$and: [{"userId": 2}, {"username": "lisi"}]})
Equivalent to the following:
> db.test1.find ({"userId": 2, "username": "lisi"})
3:$or: contains multiple conditions. The relationship between them is or. $nor is equivalent to or inversion.
The operation mode is similar to that of $and
4:$not: used on top of other conditions and reversed
> db.test1.find ({"userId": {$not: {$ne:3}}); > db.test1.find ({"userId": {$not:/1/}})
5:$mod: divides the query value by the first given value. If the remainder is equal to two equal values, the match is successful.
> db.test1.find ({"age": {$mod: [100prime3]}})
Description:
Age divided by 100. if the remainder is 3, the condition is satisfied.
6:$in: query multiple values of a key, as long as the key matches one of them. $nin does not contain
> db.test1.find ({"userId": {$in: [1J2]}})
7:$all: keys need to match all values
> db.test1.find ({"userId": {$all: [1J2]}})
8:$exists: check whether a key exists. 1 indicates it exists, 0 indicates it does not exist.
> db.test1.find ({"userId": {$exists:1}})
9:null type: matches not only the value of the key to null, but also the situation where the key does not exist
> db.test1.find ({"age": null})
III. Regular expressions
MongoDB uses Perl-compatible regular expressions (PCRE), such as:
Db.users.find ({"name": / sishuok/i}); for example:
Db.users.find ({"name": / ^ sishuok/})
Fourth, query array
1: a single element matches, just like the previous write condition, {key:value}
> db.test1.find ({"score": 5})
2: multiple elements are matched. Use $all, {key: {$all: [an all b]}}. The order of the elements does not matter.
> db.test1.find ({"score": {$all: [7 all 2]}})
3: you can use the index to specify the specific location of the query array, {"key. Index number": value}
> db.test1.find ({"score.1": 7})
4: query an array of a certain length, using $size
> db.test1.find ({"score": {$size:5}})
5: specify a subset, using $slice. The positive number is the first number, and the negative number is the tail number. You can also specify the offset and the number of elements to be returned, for example: $slice: [50 slice 10]
> db.test1.find ({}, {"score": {$slice:2}})
Specify the offset and the number of elements to return, for example: offset 1, which returns two elements:
> db.test1.find ({}, {"score": {$slice: [1pm 2]}})
6: you can use $to specify any array element that meets the criteria, such as: {"users.$": 1}
> db.test1.find ({"score": {$in: [2 score.$ 5]}}, {"score.$": 1})
7:$elemMatch: requires multiple conditional statements to compare and judge an array element at the same time
> db.test1.find ({"score": {$gt:5,$lt:4}); {"_ id": ObjectId ("5925a29652b61a20c53dfd48"), "score": [7]} >
Note: the above statement is not what we want. "greater than 5, less than 4" should not exist. If you want it to take effect, which is equivalent to and, use: $elemMatch.
> db.test1.find ({"score": {$elemMatch: {$gt:5,$lt:4}}); >
Fifth, query embedded documents
1: querying the whole embedded document is the same as an ordinary query
2: if you want to specify a key-value match, you can use "." Operators, such as: {"name.first": "a", "name.last": "b"}
> db.test1.find ({"user.id": 1}); {"_ id": ObjectId ("5925a2a552b61a20c53dfd49"), "user": {"id": 1, "username": "zhangsan"}} >
3: if you want to specify a set of conditions correctly, you may need to use $elemMatch to match multiple keys in an embedded document
Only key values in embedded documents are arrays
Note: queries for embedded documents must match the entire document exactly
4:$where query
Execute arbitrary JavaScript in the query, solve the matching problem of the query by programming, and the method returns the Boolean value.
> function T1 () {... For (var an in this) {... If (a.userId==2) {... Return true;...}...}... Return false;...}; > db.test1.find ({$where:t1})
If true is returned, the document is returned as part of the result; if false, it is not returned.
When in use: db.users.find ({"$where": T1})
Note: $where has poor performance and security is a problem, so don't use it unless you have to.
You can also go like this:
> db.test1.find ({$where: "this.userId==1"})
VI. Paging and other related
6.1. command for querying the number of records: count
1: if you directly use count (), you will get the total number of records.
Db.test1.find () .count (); 4
2: if you want to obtain the number of records after a conditional query, you need to specify count (true or non-zero number)
> db.test1.find () .limit (2) .count (); 4 > db.test1.find () .limit (2) .count (1); 2 >
6.2. command to limit the number of records returned: limit (number of records to be returned)
> db.test1.find () .limit (2); {"_ id": ObjectId ("5925a29652b61a20c53dfd48"), "userId": "1", "age": 13, "score": [7,7,2,5,4]} {"_ id": ObjectId ("5925a2a552b61a20c53dfd49"), "user": {"id": 1, "username": "zhangsan"}} >
6.3. command to limit the starting point of the number of records returned: skip (from which article to return)
> db.test1.find () .skip (2) .limit (2); {"_ id": ObjectId ("5926b15e52b61a20c53dfd4a"), "user": {"id": 2, "username": "lisi"}} {"_ id": ObjectId ("5926b6ce31e6d30b1b5084cd"), "userId": 5, "name": "zhangsan"} >
6.4.The command for sorting: sort ({Fields to be sorted: 1 is ascending,-1 is descending})
> db.test1.find () .sort ({"userId": 1})
Sort multiple fields:
> db.test1.find () .sort ({"userId": 1, "username": 1})
MongoDB processes different types of data in a certain order. Sometimes a key has multiple types of values, and the sort order is predefined, from small to large as follows:
(1) minimum (2) null (3) numeric (4) string
(5) object / document (6) Array (7) binary data (8) object id
(9) Boolean type (10) date type (11) timestamp (12) regular expression
(13) maximum
6.5.Paging query: use limit,skipt and sort together
When the amount of data is relatively small, it is recommended to use it. When the amount of data is relatively large, it is recommended to use other ways to page, such as using custom id, and then paging according to id.
Skipt retrieval method: if there are 100 pieces of data, I want Article 59, then skipt will also retrieve the first 58 items, only returned in Article 59.
6.6. Query all non-duplicated data of a given key, command: distinct
Syntax: db.runCommand ({"distinct": collection name, "key": "get fields that are not duplicated"})
> db.runCommand ({"distinct": "test1", "key": "userId"}); {"values": ["1", 5], "ok": 1}
7. Vernier
1: get cursors. Examples are as follows:
> var c = db.test1.find ()
2: circular cursors, which can be collected. Examples are as follows:
> while (c.hasNext ()) {... Printjson (c.next ());...}
3: you can also use forEach to loop. Examples are as follows:
> c.forEach (function (obj) {... Printjson (obj);...})
Effect:
VIII. Stored procedures
1:MongoDB 's stored procedure is actually a custom js function.
> var addf = function (a _ r _ b) {... Return axibten.} >
2: use db.system.js.save ({"_ id": name, value: function}) to store functions
> db.system.js.save ({"_ id": "myF", "value": addf})
3: you can view it with the following command: db.system.js.find ()
4: you can call it with the following command: db.eval (name)
Note: avoid using cursors and cursors as much as possible
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.