1. Specify the key to return
Db. [Documentname].find ({condition},{key specified})
Data Preparation Persons.json
var persons = [{
Name: "Jim",
age:25,
Email: "[email protected]",
c:89,m:96,e:87,
Country: "USA",
books:["JS", "C + +", "ExtJS", "MONGODB"]
},
{
name: "Tom",
age:25,
Email: "[email protected]",
c:75,m:66,e:97,
Country: "USA",
books:["PHP", "JAVA", "ExtJS", "C + +"]
},
{
Name: "Lili",
age:26,
Email: "[email protected]",
c:75,m:63,e:97,
Country: "USA",
books:["JS", "JAVA", "C #", "MONGODB"]
},
{
Name: "Zhangsan",
age:27,
Email: "[email protected]",
c:89,m:86,e:67,
Country: "China",
books:["JS", "JAVA", "ExtJS", "MONGODB"]
},
{
name: "Lisi",
age:26,
Email: "[email protected]",
c:53,m:96,e:83,
Country: "China",
books:["JS", "C #", "PHP", "MONGODB"]
},
{
Name: "Wangwu",
age:27,
Email: "[email protected]",
c:45,m:65,e:99,
Country: "China",
books:["JS", "JAVA", "C + +", "MONGODB"]
},
{
Name: "Zhaoliu",
age:27,
Email: "[email protected]",
c:99,m:96,e:97,
Country: "China",
books:["JS", "JAVA", "ExtJS", "PHP"]
},
{
Name: "Piaoyingjun",
age:26,
Email: "[email protected]",
c:39,m:54,e:53,
Country: "Korea",
books:["JS", "C #", "ExtJS", "MONGODB"]
},
{
Name: "Lizhenxian",
age:27,
Email: "[email protected]",
c:35,m:56,e:47,
Country: "Korea",
books:["JS", "JAVA", "ExtJS", "MONGODB"]
},
{
Name: "Lixiaoli",
age:21,
Email: "[email protected]",
c:36,m:86,e:32,
Country: "Korea",
books:["JS", "JAVA", "PHP", "MONGODB"]
},
{
Name: "Zhangsuying",
age:22,
Email: "[email protected]",
c:45,m:63,e:77,
Country: "Korea",
books:["JS", "JAVA", "C #", "MONGODB"]
}]
For (var i = 0;i<persons.length;i++) {
Db.persons.insert (persons[i])
}
1.1 The specified key for all data is queried (name, age, country)
Db.persons.find ({},{name:1,age:1,country:1,_id:0})
2. Query criteria
2. Query criteria
2.1 Check out students aged between 25-27 years
Db.persons.find ({age: {$gte: $lte: 27},{_id:0,age:1})
2.2 Find out all the math scores of students who are not Korean
Db.persons.find ({country:{$ne: "Korea"}},{_id:0,m:1})
3. Contains or does not contain
$in or $nin
2.3 Enquiries about nationality is Chinese or American student information
Db.persons.find ({country:{$in: ["USA", "China"]})
2.4 Enquiries about nationality is not a student information in China or USA
Db.persons.find ({country:{$nin: ["USA", "China"]})
4.OR Query
$or
2.4 Enquiries about students with language scores greater than 85 or English greater than 90
Db.persons.find ({$or: [{c:{$gte: 85}},{e:{$gte: 90}}]},{_id:0,c:1,e:1})
5.Null
Add new key sex to Chinese students
Db.person.update ({country: "China"},{$set: {sex: "M"}},false,true)
2.5 query for a student with sex equal to null
Db.persons.find ({sex:{$in: [Null]}},{country:1})
6. Regular queries
2.6 Find information about students with "Li" in their names
Db.persons.find ({name:/li/i},{_id:0,name:1})
7. Use of $not
$not can be used anywhere to reverse the operation
2.7 Find information about students whose name does not exist in "Li"
Db.persons.find ({name:{$not:/li/i}},{_id:0,name:1})
The difference between $not and $nin is that $not can be used anywhere, $nin is used on a set.
8. Array query $all and index applications
2.8 Queries students who like to see Mongod and JS
Db.persons.find ({books:{$all: ["MONGOBD", "JS"]}},{books:1,_id:0})
2.9 Query The second book is the Learning information of Java
Db.persons.find ({"Books.1": "JAVA"})
9. Querying a specified length array $size it cannot be used with a comparison query (this is a disadvantage)
2.8 The number of books I like is 4 students
Db.persons.find ({books:{$size: 4}},{_id:0,books:1})
2.9 Students who like more than 3 books
1. Increase the field size
Db.persons.update ({},{$set: {size:4}},false,true)
2. Change the way books are updated, size increases by 1 each time you add a book
Db.persons.update ({},{$push: {books: "ORACLE"}, $inc: {size:1}})
3. Using $GT Query
Db.persons.find ({size:{$gt: 3}})
2.10 Using the shell to find out the number of books Jim likes to read
var persons = Db.persons.find ({name: "Jim"})
while (Persons.hasnext ()) {
obj = Persons.next ();
Print (Obj.books.length)
}
1.mongodb is a NoSQL database, but he's still very powerful in document queries.
2. The query is basically used in curly braces inside the update character is basically outside
3.shell is a thoroughly-bottomed JS engine, but some special operations depend on his
Each driver package to complete (java,node. Msn
Db.persons.find ({age: {$gte: $lte: 27}},{"_id": ObjectId ("53ffe4a7504a4886bc371029"), age:1}) for students aged between 25-27 years Find out the math scores of all students who are not Korean Db.persons.find ({country: {$ne: "Korea"}},{"_id": ObjectId ("53ffe4a7504a4886bc371029"), age:1} )//Query nationality is Chinese or American student information Db.persons.find ({country:{$in: ["USA", "China"]})//Query nationality is not Chinese or American student information Db.persons.find ({country:{ $nin: ["USA", "China"]})//query for student information with a language score greater than 85 or English greater than 90 db.persons.find ({$or: [{c:{$gte: 85}},{e:{$gte: 90}}]},{_id:0,c:1 , e:1})//Add a new key to Chinese nationality sexdb.persons.update ({country: "China"},{$set: {sex: "MS"}},false,true) db.persons.update ({ Country: "USA"},{$set: {set: "MS"}},true,true) db.persons.update ({country: "China"},{$set: {sex: "M"}},false,true) Db.persons.update ({age:27},{$set: {status: "A"}},{Multi:true})//query out the sex equals null student Db.persons.find ({sex:{$in: [null ]}},{COUNTRY:1})//query out the name of the student with "Li" information, regular query Db.persons.find ({name:/li/i},{_id:0,name:1})//query out the name does not exist "Li" The student's information Db.persons.find ({name:{$not:/li/i}},{_id:0,name:1})//query likes to see Mongod and JS students Db.persons.find ({books:{$all: [" Mongobd"," JS "]}},{books:1," _id ": ObjectId (" 53ffe4a7504a4886bc371029 ")})//query likes to see Mongod and JS students Db.persons.find ({" Books.1 ":" JAVA "})//query out the number of books you like is 4 student Db.persons.find ({books:{$size: 4}},{_id:0,books:1})//Add field sizedb.persons.update ({},{$ Set:{size:4}},false,true) db.persons.update ({name: "Tom"},{$push: {books: "ORACLE"}, $inc: {size:1}})//use $ GT Query Db.persons.find ({size:{$gt: 4})//Use the shell to find out the number of books Jim likes to read var persons = Db.persons.find ({name: "Jim"}) while ( Persons.hasnext ()) {obj = Persons.next ();p rint (Obj.books.length)}
Disclaimer: This blog attaches great importance to the protection of intellectual property rights, found that the information posted by this blog contains a violation of its copyright link content, please contact me, I will be the first time to do the corresponding processing, contact email [email protected].
Mark Fan (Small idea) Source: http://cube.cnblogs.com
Note: This statement must be retained without the consent of the author, and the original text connected in the obvious position of the article page, otherwise the right to hold the legal responsibility. In case of doubt, you can contact the author via [email protected], this article is licensed under the Creative Commons Attribution-NonCommercial use-share 2.5 China mainland license agreement in the same way
MongoDB's Find details (i)