MongoDB Query operation

Source: Internet
Author: User

1 Data preparation

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])
}
var persons = Db.persons.find ({name: "Jim"})

2 Query 2.1 queries out the specified key for all data (name, age, country)

Db.persons.find ({},{name:1,age:1,country:1,_id:0})

2.2 Query criteria

Check out students aged between 25-27 years

Db.persons.find ({age: {$gte: $lte: 27},{_id:0,age:1})

Find out the math scores of all students who are not Korean

Db.persons.find ({country:{$ne: "Korea"}},{_id:0,m:1})

2.3 Contains or does not contain: $in or $nin

Check nationality is a student's information in China or USA

Db.persons.find ({country:{$in: ["USA", "China"]})

Check nationality is not a Chinese or American student information

Db.persons.find ({country:{$nin: ["USA", "China"]})

2.4 or query: $or

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})

2.5 Null

Add new key sex to Chinese students

Db.person.update ({country: "China"},{$set: {sex: "M"}})

Query for students with sex equal to null

Db.persons.find ({sex:{$in: [Null]}},{country:1})

2.6 Regular queries

Find information about students with "Li" in their names

Db.persons.find ({name:/li/i},{_id:0,name:1})

The use of 2.7 $not, $not can be used anywhere to take the reverse operation

Find information about students who do not have "Li" in their name

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.

2.8 Array Query $all and index applications

Find students who like to see Mongod and JS

Db.persons.find ({books:{$all: ["MONGOBD", "JS"]}},{books:1,_id:0})

Query the second book is the Learning information of Java

Db.persons.find ({"Books.1": "JAVA"})

Queries the specified length array $size it cannot be used with comparison queries (this is a disadvantage)

Check out the number of books you like that are 4 students

Db.persons.find ({books:{$size: 4}},{_id:0,books:1})

2.9 Students who like more than 3 books

Increase Field Size

Db.persons.update ({},{$set: {size:4}},false, True)

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}})

Using $GT Query

Db.persons.find ({size:{$gt: 3}})

2.10 $slice operator returns the internal value of the specified array in the document

Find out the 2nd to 4th book in Jim's Bookshelf

Db.persons.find ({name: "Jim"},{books:{"$slice": [1,3]}})

Check out the last book

Db.persons.find ({name: "Jim"},{books:{"$slice": -1},_id:0,name:1})

2.11 Document query: $elemMatch

Add a resume document to Jim

var Jim = [{
School: "K",
Score: "A"
},{
School: "L",
Score: "B"
},{
School: "J",
Score: "A +"
}]
Db.persons.update ({name: "Jim"},{$set: {school:jim}})

Check out the students who have studied at K.

Db.persons.find ({school:{$elemMatch: {school: "K", Score: "A"}})

2.12 Limit returns the specified number of data bars

Querying the first 5 data in a persons document

Db.persons.find ({},{_id:0,name:1}). Limit (5)

2.13 Skip returns the span of the specified data

Querying the data for 5~10 bars in a persons document

Db.persons.find ({},{_id:0,name:1}). Limit (5). Skip (5)

2.14. Sort returns data sorted by age [1,-1]

Db.persons.find ({},{_id:0,name:1,age:1}). Sort ({age:1})

MongoDB Query operation

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.