MongoDB Query Find(2012-01-05 11:38:10)
reproduced
Tags:it |
Category: MongoDB |
MongoDB Query Find
Specifies the key to return, specifying the desired key through the second parameter of Find or FindOne.
> db.visithomepage.find ({},{"user_id": 1, "Time": 1})
{"_id": ObjectId ("4e0713be3ded0ab30ccb4b81"), "user_id": Numberlong (22715364), "Time": Numberlong (1305108928)}
Change what you don't need to see to 0, such as change _id to 0, and not let him reply.
> db.visithomepage.find ({},{"user_id": 1, "Time": 1, "_id": 0}) {"user_id": Numberlong (23020418), "Time": Numberlong (1305105014)}
Restriction: You cannot reference the values of other keys in the document.
--------------------------------------------------------------------------------
Query criteria:
$lt, $lte, $gt, $gte, $ne < <= > >=≠> db.visithomepage.find ({"user_id": {"$lte": 23011003, "$gte": 2301000 0}})
Lookup Date start = new Date ("01/01/2007") Db.visithomepage.find ({registered ": {" $lt ": Start}})
or query $in and $or > Db.visithomepage.find ({"user_id": {"$in": [23011003, 23010000]}})
And of course, $nin.
$in can make queries on a single key. If you do a query on multiple keys, you need to use $or, $or optimization is that the first condition is likely to match more documents.
> Db.visithomepage.find ({"$or": [{"user_id": 23011003}, {"Time": 1305115967}]})
$nin data db.c1.find ({age:{$nin: [7,8]}}) that does not contain the value of the query age outside the 7,8 range;
--------------------------------------------------------------------------------
$not
He is a meta-condition that can be used on any other condition.
For example, the $mod modulo operator, Db.users.find ({"user_id": {"$mod": [5, 1]}}) The query above returns users such as 1,6,11,16. If you want to return to 2,3,4,5,7,8,9,10,12 and other users will use $not Db.users.find ({"user_id": {"$not": {"$mod": [5, 1]}})
--------------------------------------------------------------------------------
Regular expression Db.users.find ({"Name":/zorpia/i}) supports Perl's Pcre library to match regular expressions
Query the name of the data db.c1.find ({name: {$not:/^t.*/}}) that does not begin with T;
--------------------------------------------------------------------------------
Query array db.zorpia.insert ({"Fruit": ["apple", "banana", "Peach"]})
Use the following query: Db.zorpia.find ({"Fruit": "Banana"})
1. $all If multiple elements are required to match the array, use $all, for example: Db.zorpia.insert ({"Fruit": ["apple", "banana", "Peach"]}) Db.zorpia.insert ({"Fruit" : ["Apple", "kumquat", "Orange"]}) Db.zorpia.insert ({"Fruit": ["Cherry", "banana", "Apple"]}) to find documents with Apple and banana, use $ All Db.zorpia.find ({"fruit": {$all: ["Apple", "Banana"]}}) If you want to query the element at the specified position in the array, use the syntax to specify the subscript, for example: Db.zorpia.find ({"Fruit.2": " Peach "}) above this will match the third element is the Peach document
2. $size array elements to query for a specified length of array db.zorpia.find ({"Fruit": {"$size": 3}}) queries 3-length arrays, but he cannot be combined with other query clauses, such as $GT
3. $slice return to the top 10 comments Db.zorpia.post.findOne (criteria, {"Comments": {$slice: 10}}) returns the last 10 Db.zorpia.post.findOne ( Criteria, {"Comments": {$slice:-10}}) returns 24-33 elements Db.zorpia.post.findOne (criteria, {"Comments": {$slice: [23,10]}}) But they're all going to return all the keys.
--------------------------------------------------------------------------------
Query the inline document "name": {"FirstName": "Joe", "LastName": "Schmoe"}
You can use the following query Joe Schmoe people, very annoying, FirstName and LastName must write the whole, and first and last order cannot point to.
> Db.users.findOne ({"name": {"FirstName": "Joe", "LastName": "Schmoe"}})
The right method to use. Number > Db.users.findOne ({"Name.firstname": "Joe"}) {"_id": ObjectId ("4e0310f40611960df1e86a95"), "emails": ["[ Email protected]","[email protected]","[email protected]"," name ": {" FirstName ":" Joe "," Las Tname ":" Schmoe "}} Note that the point notation is also the document to be inserted cannot contain". ”。 If we save the URL, we'll run into problems. The workaround is to perform a global substitution before or after the insert. Replace with an illegal character in the URL.
When the document structure is complex. Suppose you have a few blog posts to find out more than 5 points of Joe's outburst. The structure of the blog post is as follows: > Db.blog.find () {"Content": "...", "comments": [{"Author": "Joe", "Score": 3, "comment": "Nice post" "}, {" Author ":" Mary "," Score ": 6," comment ":" 6 Fen "}]} can only be matched with $elemmatch. This ambiguous naming condition can be used to partially specify the qualifying conditions for a single inline document in the matched array. Db.blog.find ({"Comments": {"$elemMatch": {"author": "Joe", "score": {"$gte": 5}}})
--------------------------------------------------------------------------------
$where P55 It can execute arbitrary JavaScript as part of the query. I'm not quite clear on this, but I'll look into it later. We need to study it ourselves.
Note: Try to avoid using $where queries. The speed is much slower than regular queries. Each document is converted from Bson to a JavaScript object and then run through the $where expression. Likewise he cannot use the index.
--------------------------------------------------------------------------------
cursors var cursor = Db.users.find () > while (Cursor.hasnext ()) {obj = Cursor.next ();}
Cursor.hasnext () Check that there are subsequent results, and then use Cursor.next () to obtain them.
can also be used foreach:var cursor = Db.users.find () Cursor.foreach (function (x) {print (x.name);});
--------------------------------------------------------------------------------
Limit,skip and sort
The limit function, for example, returns only 3 results. Db.users.find (). Limit (3) limit is the upper bound, not the lower limit
Skip is similar to limit: Db.users.find (). Skip (3) skips over the first three matching documents and then returns the remaining documents. If the collection has fewer than three documents, no documents are returned.
Sort uses an object as an argument: 1 for ascending, 1 descending. If more than one key is specified, it is sorted in order of multiple keys. Db.users.find (). Sort ({"username": 1, "Age":-1})
The above three methods can be combined with each other. Handling paging is useful. For example, someone wants to search for MP3. If 50 results per page are returned and sorted by Price: Db.stock.find ({"desc": "MP3"}). Limit ((). Sort ({"Price":-1})
On the next page, just skip 50. Db.stock.find ({"desc": "MP3"}). Skip. Limit ({"Price":-1})
You should avoid skipping too many results.
Statistical Db.things.count (); Select COUNT (*) from things; Db.things.find ({x:10}). Count (); Select COUNT (*) from things where x=10; The following return is not 5, but is the number of records in the user Table Db.users.find (). Skip (5). Count (); If you want to return the number of records after the limit, use count (true) or count (not 0) db.users.find (). Skip. Limit (5). Count (True);
Multi-Criteria Query Db.things.find ({x:3, y: "foo"}); where x = 3 and y = "foo"
Specifies that the result set condition Db.things.find () is returned. Limit (10); Limit, Db.things.find (). Skip (7). Limit (10); Limit 7, 10
Sort Db.things.find (). Sort ({$natural: 1}); ORDER by _id ASE Db.things.find (). Sort ({$natural:-1}); ORDER by _id Dese Db.things.find (). Sort ({x:1}); ORDER by x ASC Db.things.find (). Sort ({x:-1}); ORDER by x DESC Db.things.find (). Sort ({x:1, Y:-1}); // ~.~
Modulo db.things.find ({x: {$mod: [10, 1]}}); where x=1
Matches a numeric element Db.things.find ({x: {$in: [2, 4, 6]}}); X in array db.things.find ({x: {$nin: [2, 4, 6]}}); X not in array
$all match all of this operator is similar to the SQL syntax in, but the difference is that in only one value of () is satisfied, and $all must
All values within [] must be met, for example: Db.users.find ({age: {$all: [6, 8]}}); Can query {name: ' David ', age:26, Age: [6, 8, 9]} but cannot query {name: ' David ', age:26, Age: [6, 7, 9]}
$exists determine if a field exists
Queries all records that exist in the age field Db.users.find ({age: {$exists: true}}); Query all records that do not exist in the Name field Db.users.find ({name: {$exists: false}});
Null Value Handling
> Db.c2.find ({age:null})
$mod modulo operations
Query age modulo 6 equals 1 of data
Db.c1.find ({age: {$mod: [6, 1]}})
$ne Not equal to
The value of the query x is not equal to 3 of the data Db.c1.find ({age: {$ne: 7}});
Javascript queries and $where queries
Querying a data greater than 3, the following query method is the same as Db.c1.find ({a: {$gt: 3}}); Db.c1.find ({$where: "THIS.A > 3"}); Db.c1.find ("THIS.A > 3"); f = function () {return THIS.A > 3;} db.c1.find (f);
Stored Procedures
The first thing you need to know about stored procedures is that it's written in JavaScript. MongoDB stored procedures are stored in the Db.system.js table, we imagine a simple SQL Custom function as follows:
function AddNumbers (x, y) {
return x + y; Below we convert this SQL custom function to MongoDB's stored procedure:
> Db.system.js.save ({_id: "AddNumbers", Value:function (x, y) {return x + y;}});
Stored procedures can be viewed, modified, and deleted, so we use find to see if the stored procedure has been created. > Db.system.js.find () Call this stored procedure: Db.eval_r (' AddNumbers (3, 4.2) ');
Db.eval_r () is a strange thing, and we can call the logic of the stored procedure directly inside and at the same time without having to declare the logic of the stored procedure beforehand.
Db.eval_r (function () {return 3+3;}
MongoDB Query Find (