MongoDB's Find () operation

Source: Internet
Author: User
Tags mongodb find

Create a database

Performing an operation into MongoDB using parameter mode can also be verified using Db.auth ().

[Email protected]:/usr/local/apache243/htdocs#  /usr/local/mongodb/mongo  -uroot-pxxxxx localhost : 27017/admin

The following is the first time a new database is created

 Use demoswitched to db DEMO> Db.user.insert ({name: "Zhaoxingzhuang", Age:18,sex: "Man", Location:{province: " Shandong ", City:" Jinan ", Road:" shandaroad-47 "" ninserted ": 1 })> Db.user.find ()  // All Queries {"_id": ObjectId ("54feea0a26be41dca9db0d23"), "name": "Zhaoxingzhuang", "age": +, "sex": "Man", "location": {"Prov Ince ":" Shandong "," City ":" Jinan "," Road ":" Shandaroad-47 " }}

Continue with the insert operation

1, simple query find ({field: "Value"},{field:0 or 1}), before the condition of the query indicates that the query field 0 means that the field is not displayed 1 indicates that only this field is displayed

> Db.user.insert ({name: "Wangping", Age:26,sex: "Woman", Location:{city: "Beijing", Road: "Zhongguonongyedaxue"}}) Writeresult ({"Ninserted": 1 })> Db.user.find () {"_id": ObjectId ("54feea0a26be41dca9db0d23"), "name": "Zhaoxingzhuang", "age": +, "sex": "Man", "location": {"Provin Ce ":" Shandong "," City ":" Jinan "," Road ":" Shandaroad-47 " } }{ "_id": ObjectId ("54feead226be41dca9db0d24"), "name": "Wangping", "age": +, "sex": "Woman", "location": {"City": "B Eijing "," Road ":" Zhongguonongyedaxue " } }> Db.user.find ({name: "Wangping"})//query Name= "wangping"{"_id": ObjectId ("54feead226be41dca9db0d24"), "name": "Wangping", "age": +, "sex": "Woman", "location": {"City": "Beijing", "Road": "Zhongguonongyedaxue" } }//The field selects Select Age from XXX in a similar SQL statement;> Db.user.find ({name: "wangping"},{age:0})//indicates that the age field is not displayed{"_id": ObjectId ("54feead226be41dca9db0d24"), "name": "Wangping", "Sex": "Woman", "location": {"City": "Beijing", " Road ":" Zhongguonongyedaxue " } }> Db.user.find ({name: "wangping"},{age:1})//indicates that only the age field is taken{"_id": ObjectId ("54feead226be41dca9db0d24"), "age": 26 }>

2, Condition Inquiry > $gt, < $lt, <= $lte, >= $gte.

> Db.user.find ({age:{$gt: +})   // query {"_id" for age greater than 20 : ObjectId (" 54feead226be41dca9db0d24 ")," name ":" Wangping "," age ": +," sex ":" Woman "," location ": {" City ":" Beijing "," Road ": "Zhongguonongyedaxue" }}

3. $all operation is only valid for the array (similar to $in), which represents the $all to query: [1,2,5,6] 1256 must be all in the arrays to be queried, and $in means that the query criteria are met whenever one is available.

> Db.user.insert ({name: "Gaofei", Age:24,grade:[1,2,5,6],location:{province: "Shandong", City: "Jiyang", Road: " Zuozhuangzhen "" ninserted ": 1 })> Db.user.find (grade:{$all: [1,2,5,6]}) 2015-03-10t21:19:20.164+0800 syntaxerror:unexpected token:> db.user.find ({grade:{$all: [1,2,5,6]}})    //$all  query condition {"_id": ObjectId ("54feef3626be41dca9db0d25"), "name": "Gaofei", "age": 24, " Grade ": [1, 2, 5, 6]," location ": {" province ":" Shandong "," City ":" Jiyang "," Road ":" Zuozhuangzhen " }}

$all $in just used on the array

> Db.user.find ({name:{$all: [1,2,5,6]}})   // field is not an array  of cases > Db.user.find ({ name:{$in: [1]})> Db.user.find ({grade:{$in: [1]})       //$in use Query {"_id": ObjectId ("54feef3626be41dca9db0d25"), "name": "Gaofei", "Age": 5, "Grade": [1, 2,, 6], "Location" : {"Province": "Shandong", "City": "Jiyang", "Road": "Zuozhuangzhen" }}

4, skip () limit () count () sort () use

Where skip and limit are used similar to the SELECT * from user limit 0, 10 in SQL statements;

> Db.user.find ()//list them in the order of addition{"_id": ObjectId ("54feea0a26be41dca9db0d23"), "name": "Zhaoxingzhuang", "age": +, "sex": "Man", "location": {"Prov Ince ":" Shandong "," City ":" Jinan "," Road ":" Shandaroad-47 " } }{ "_id": ObjectId ("54feead226be41dca9db0d24"), "name": "Wangping", "age": +, "sex": "Woman", "location": {"City": "B Eijing "," Road ":" Zhongguonongyedaxue " } }{ "_id": ObjectId ("54feef3626be41dca9db0d25"), "name": "Gaofei", "Age": 5, "Grade": [1, 2,, 6], "location": {"pro Vince ":" Shandong "," City ":" Jiyang "," Road ":" Zuozhuangzhen " } }//Limit Zero-based query two> Db.user.find (). Limit (2){ "_id": ObjectId ("54feea0a26be41dca9db0d23"), "name": "Zhaoxingzhuang", "age": +, "sex": "Man", "location": {"Provin Ce ":" Shandong "," City ":" Jinan "," Road ":" Shandaroad-47 " } }{ "_id": ObjectId ("54feead226be41dca9db0d24"), "name": "Wangping", "age": +, "sex": "Woman", "location": {"City": "B Eijing "," Road ":" Zhongguonongyedaxue " } }> Db.user.skip (1).limit ()2015-03-10t21:26:17.430+0800 typeerror:property ' Skip ' ofObjectDemo.user is not afunction> Db.user.find (). Skip (1). Limit ()//start with the first one .{"_id": ObjectId ("54feead226be41dca9db0d24"), "name": "Wangping", "age": +, "sex": "Woman", "location": {"City": "Beijing", "Road": "Zhongguonongyedaxue" } }{ "_id": ObjectId ("54feef3626be41dca9db0d25"), "name": "Gaofei", "Age": 5, "Grade": [1, 2,, 6], "location": {"pro Vince ":" Shandong "," City ":" Jiyang "," Road ":" Zuozhuangzhen " } }> Db.user.Count()//Total Statistical Data3> Db.user.find ().Sort(age:1)//you have to put the sorted contents in the {}2015-03-10t21:27:49.863+0800 syntaxerror:unexpected token:> db.user.find ().Sort({age:1}){ "_id": ObjectId ("54feea0a26be41dca9db0d23"), "name": "Zhaoxingzhuang", "age": +, "sex": "Man", "location": {"Provin Ce ":" Shandong "," City ":" Jinan "," Road ":" Shandaroad-47 " } }{ "_id": ObjectId ("54feef3626be41dca9db0d25"), "name": "Gaofei", "Age": 5, "Grade": [1, 2,, 6], "location": {"pro Vince ":" Shandong "," City ":" Jiyang "," Road ":" Zuozhuangzhen " } }{ "_id": ObjectId ("54feead226be41dca9db0d24"), "name": "Wangping", "age": +, "sex": "Woman", "location": {"City": "B Eijing "," Road ":" Zhongguonongyedaxue " } }>

MongoDB find () operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.