Simple use of MongoDB-query operations

Source: Internet
Author: User
Tags mongodb

MongoDB performs a query operation through the Find () function

1. Find all the documents in the collection:

Db.media.find ()

System returns:

{"_id": ObjectId ("5aa47c1953350e04ddbd6fa3"), "Type": "Book", "Title": "Definitive Guide to MongoDB 3rd ed.", "ISBN": "978-1-4842-1183-0", "Publisher": "Apress", "Author": ["Hows,david", "Plugge,eelco", "Membrey,peter", "Hawkins,tim"] }
{"_id": ObjectId ("5aa47dd553350e04ddbd6fa4"), "Type": "CD", "Artist": "Nirvana", "Title": "Nevermind", "tracklist": [{' track ': ' 1 ', ' titie ': ' Smells like Teen Spirit ', ' Length ': ' 5:02 '}, {' track ': ' 2 ', ' Title ': ' In Bloom ', ' lengt H ":" 4:15 "}]}

Shows the 2 documents we added in the previous article

2. Find all CDs for Nirvana:

Db.media.find ({Artist: "Nirvana"})

System returns:

{"_id": ObjectId ("5aa47dd553350e04ddbd6fa4"), "Type": "CD", "Artist": "Nirvana", "Title": "Nevermind", "tracklist": [{' track ': ' 1 ', ' titie ': ' Smells like Teen Spirit ', ' Length ': ' 5:02 '}, {' track ': ' 2 ', ' Title ': ' In Bloom ', ' lengt H ":" 4:15 "}]}

3. Find the title of all CDs for Nirvana:

Db.media.find ({Artist: "Nirvana"},{title:1})

System returns:

{"_id": ObjectId ("5aa47dd553350e04ddbd6fa4"), "Title": "Nevermind"}

Find non-header information for all CDs of Nirvana

Db.media.find ({Artist: "Nirvana"},{title:0})

System returns:

{"_id": ObjectId ("5aa47dd553350e04ddbd6fa4"), "Type": "CD", "Artist": "Nirvana", "tracklist": [{"Track": "1", "tit ie ":" Smells like Teen Spirit "," Length ":" 5:02 "}, {' track ':" 2 "," Title ":" In Bloom "," Length ":" 4:15 "}]}

4. Find information about the inline object document:

Db.media.find ({"Tracklist.title": "In Bloom"})

The system returns all the information for the corresponding document

{"_id": ObjectId ("5aa47dd553350e04ddbd6fa4"), "Type": "CD", "Artist": "Nirvana", "Title": "Nevermind", "tracklist": [{' track ': ' 1 ', ' titie ': ' Smells like Teen Spirit ', ' Length ': ' 5:02 '}, {' track ': ' 2 ', ' Title ': ' In Bloom ', ' lengt H ":" 4:15 "}]}

5 Other related functions

Sort () sorting function db.media.find (). Sort ({title:1}) #1为升序, 1 is descending

Limit () limits the maximum number of returned results db.media.find (). Limit (Ten) #0表示返回所有结果

Skip () ignores the first few documents in the collection Db.media.find (). Skip (20)

Simple use of MongoDB-query operations

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.