MongoDB Basic Command Summary

Source: Internet
Author: User
Tags findone mongodb limit

In fact, I always want to tidy up the MongoDB database I often use some operations command, finally have time ~

MongoDB is an open source, free, non-relational database (NOSQL) with no tables, no records, and some differences from the usual relational database:

    • The collection in MongoDB can be likened to a table in a relational database, and the collection has no fixed structure, which means that you can insert different formats and types of data into the collection, but usually the data we insert into the collection will have some relevance. The collection is stored in the database.
    • A document in MongoDB can be likened to a field in a table in a relational database, where the document is primarily stored in a collection, and MongoDB stores the data as a document, and the structure is composed of key-value (key=>value) pairs. A MongoDB document is similar to a JSON object. Field values can contain other documents, arrays, and array of documents.

Here are some common commands

First, the database

1. View all databases: Show DBS

2. Switch Database:use database_name # If the database does not exist, create the database

3. Delete database:db.dropdatebase ()

Two, set

1 . View All collections:show collections or show tables

2. Create Mongotest collection:

A, Db.creatcollection ("Mongotest") # Create a collection

B, Db.mycollections.insert ({"Name": "Hum ~"}) # in MongoDB, when you insert some documents, MongoDB automatically creates the collection

Third, the document

1. Insert Document:db. Mycollections.insert () or db.mymycollections.save ()

# If you do not specify the _id field, the Save () method is similar to the Insert () method. If the _id field is specified, the data for the _id is updated.

Cases:

2 . Find document:db. Mycollections.find ()

Db. Mycollections.find (). Pretty () # Find all documents, output by format

Db.mycollections.findOne () # FindOne is arranged according to the _id sequence, without the query criteria, to find the first inserted data

Example:        

Other related query condition syntax is as follows:

    • Less than: syntax {<key>:{$lt: <value>}}, Example: Db.mycollections.find ({"Age": {$lt: 19}})
    • Less than or equal to: {<key>:{$lte: <value>}}, Example: Db.mycollections.find ({"Age": {$lte: 19}})
    • Greater than: {<key>:{$gt: <value>}}, Example: Db.mycollections.find ({"Age": {$gt: 19}})
    • Greater than or equal to: {<key>:{$gte: <value>}}, Example: Db.mycollections.find ({"Age": {$gte: + }})
    • Not equal to: {<key>:{$ne: <value>}}, Example: Db.mycollections.find ({"Age": {$ne: 18}})

3. Update command: db.mycollections.update ()    

Example: The above Wang Hong age from 18 to 19 years old, Operation command for: db.mycollections.update ({"Name": "Wang Hong"},{$set: {"Age": "}} "

4.To Delete a document: db.mycollections.remove () Example: 1, delete "Zhang San", the Operation command is: Db.mycollections.remove ({"Name": "Zhang San"}) 2, insert more than one named "Zhang San", delete the found 1th "Zhang San", The action command is: Db.mycollections.remove ({"Name": "Zhang San"},1)# 1,justone Parameters

3. Insert more than one person named "Zhang San", delete the first 2 "Zhang San" found, the Operation command is:

    Db.mycollections.remove ({"_id": {$in: [ObjectId ("5b86b20c873007ee4d98c1e6"), ObjectId (" 5b86b20e873007ee4d98c1e7")]})



Iv. Other Orders

1. Query filtering

  Db.mycollections.find ({"Name": "Zhang San"},{"Age": 1,_id:0}) # 1 indicates that name,0 is displayed without _id.

2. Limit ()

If you need to read a specified number of data records in MongoDB, you can use the MongoDB limit method, and the limit () method accepts a numeric parameter that specifies the number of record bars read from MongoDB.

Db.mycollectons.find (). Limit (2) # shows only 2 documents

3. Skip ()

In addition to using the limit () method to read a specified amount of data, you can also use the Skip () method to skip a specified number of data, and the Skip method also accepts a numeric parameter as the number of skipped records. Db.mycollections.find ({},{"name": 1,_id:0}). Limit (1). Skip (1) # Show only the second document
4. Sort ()MongoDB uses the sort () method to sort the data, and the sort () method can specify the sorted fields by parameter and use 1 and-one to specify how the sort is sorted, where 1 is in ascending order, and 1 is used for descending order. Db.mycollections.find (). Sort ({age:1}) # documents sorted by age, ascending

MongoDB Basic Command Summary

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.