MongoDB Common directives

Source: Internet
Author: User
Tags mongodb

  • db
    View the database for the current operation

  • show dbs
    Show all databases

  • show collections
    Displays all collections under the current database

  • use database_name
    Connect to a database named "database_name" and create if it does not exist

  • db.dropDatabase()
    Delete current Database

  • db.collection_name.drop()
    Delete named "Collection_name" collection

  • db.createCollection(‘collection_name‘ , {capped : true ,size : 100000} )
    Create a collection called "collection_name", specifying a length of 100000b

  • db.collection_name.find({})
    The query is called "collection_name" in the collection of all documents, curly braces for the query criteria, unconditional query all.

  • db.collection_name.find({}).pretty()
    Query all the documents in the collection, pretty to beautify the display effect.

  • db.collection_name.find({}).limit(1).skip(2)
    Pagination Displays the result of the query, limit is the starting position, and skip is the number of bars taken.

  • db.collection_name.find({}).sort({KEY:1})
    Sort for sorting criteria, 1 for ascending, 1 for descending

  • db.collection_name.insert({‘name‘:‘yan‘})
    Inserts a document into the collection "collection_name".

  • ' Db.collection_name.save ({}) '
    Save the data, and if you do not specify an ID, the effect is the same as insert, which is the updated action if the ID is specified.

  • db.collection_name.update({<query>,<update>,{upsert:<boolean>,multi:<boolean>,writeConcern:<document>}})
    Update document
    • Query: Search criteria
    • Update: Fields that need to be updated
    • Upsert: Optional (false by default), set to true: Inserts are made if there are no documents that meet the criteria of the query.
    • Multi: Optional (false by default), set to true: if there are more than one document that meets the query criteria, all of the documents are updated. Set false: Updates only the first document to the query.
    • Writeconcern: Optional, throws an exception level
  • db.collection_name.remove(<query>,{justOne:<boolean>,writeConcern:<document>})
    Delete

  • db.collection_name.ensureIndex({KEY:1})
    Index, can write multiple (composite index)

  • db.collection.aggregate( [ $group : { _id : ‘$__xh‘ , num_tutorial : {$sum : 1} } ] )
    Aggregation, which is an advanced query, for example, the total number of statistical classes

MongoDB Common directives

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.