MongoDB query data

Source: Internet
Author: User
Tags mongodb query

MongoDB Concept Analysis:

Database tables that are equivalent to SQL: Collection
Document: Data record lines that are equivalent to SQL
Field: Data field equivalent to SQL
Table connection, MongoDB not supported
Primary key, MongoDB automatically sets the _id field as the primary key

1. Querying data (documents)

Db.mycol.find () Db.mycol.find (). Pretty (   ) // format Display db.mycol.findOne ()      // return a record

2. View a few records

Db.mycol.count ()

3. Query the required fields:

 Db.mycol.find ({},{ " title   ": 1 }" // Span style= "COLOR: #008000" To display only the ID and title  db.mycol.find ({},{ " title   ": 1 ,  " by  " : Span style= "COLOR: #800080" >1 }) //  display ID, Title and by  Db.mycol.find ({},{ " title   ": 1 , _id: 0 }) //  Show only title  

4. Conditional query Specifies the key returned

Db. [Documentname].find ({condition},{key specified})
Data preparation? Persons.json

5.db.person.find ({null = query all},{_id:0,name:1,country:1})//_id:0 indicates that _id is not displayed

5.1 The specified key for all data is queried (name, age, country)

Db.persons.find ({},{name:1, Age:1, Country:1, _id:0})

5.2 Check out students aged between 25-27 years

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

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

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

6. Contains or does not contain

$in or $nin

6.1 Enquiries about nationality is Chinese or American student information

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

6.2 Enquiries about nationality is not a student information in China or USA

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

7.OR Query

$or
7.1 Enquiries about students with language scores greater than 85 or English greater than 90

Db.persons.find ({$or: [{c:{$gte:}},{e:{$gte:}}]},{_id:0, C:1, E:1 })

8.Null

Add new key sex to Chinese students

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

8.1 Query for a student with sex equal to null

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

9. Regular queries

9.1 Find information about students with "Li" in their names

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

Use of $not

$not can be used anywhere to reverse the operation
10.2 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 the collection 

11. Array query $all and index applications

11.1 Queries students who like to see Mongod and JS

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

11.2 Query The second book is the Learning information of Java

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

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

12.1 The number of books I like is 4 students

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

12.2 Students who like more than 3 books

1. Increase the field size

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

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

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 ()) {    = persons.next ();        

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

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

13.1 Find out the 2nd to 4th book in Jim's bookshelf.

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

13.2 Check out the last book

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

14. Document Query

Add a curriculum vitae document to Jim Jim.json
Check out the students who have studied at K.
1. We can do this with an absolute match, but there are some problems (looking for a problem?). Always take score with you?)

Db.persons.find ({school:{school:"K", Score:"A"}},{_id: 0, School:1})

2. In order to solve the problem of order I can use the object "." The way to locate

Db.persons.find ({"school.score":"A","  School.school":"K"},{_id:0, School:1})

3. So also the question looks example:

Db.persons.find ({"school.score":"A","  School.school":" J "},{_id:0, School:1})

The same data can be found, because score and school are going to compare to other objects.
4. Correct practice single condition group query $elemmatch

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

MongoDB query data

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.