MongoDB General Command

Source: Internet
Author: User
Tags mongodb tutorial

  • 1. Common commands
      • show DBS Show all databases
      • use dbname into dbname database, if not present,
      • db View database
      • show collections displays the collection in the database--equivalent to the table in MySQL!
  • Create & Add
      • db.users.save ({" Name ":" LECAF "}), created a collection called Users,
      • db.users.insert ({" Name ":" Ghost "," Age ": 10}) inserts a new data into the Users collection, If you do not have the Users collection, MongoDB automatically creates
      • save () and insert () also have a slight difference: if the new data primary key already exists, insert () will not do the operation and prompt error, and save () The original content is changed
  • 3. Delete
    • DB. Dropdatabase() Deleting a database
    • Db.users.remove () Delete all data under the Users collection
    • Db.users.remove ({"Name": "LECAF"}) deletes data NAME=LECAF under the Users collection
    • Db.users.drop () or Db.runcommand ({"Drop", "users"}) Delete collection users
    • Db.runcommand ({"Dropdatabase": 1}) Delete current data
  • 4. Find
    • Db.users.find (). Pretty () formatting data
    • Db.users.find () Find all data in the Users collection
    • Db.users.findOne () to find the first number in the Users collection
    • Db.book.find ({' BookName ': ' Shuihuzuan '}) equals query
    • db.book.find ({' Bookprice ': {$lt: 10000}}) is less than the query
        • (>) greater than-$gt
          • (<) less than-$lt
          • (>=) greater than or equal-$gte
          • (<=) less than or equal-$lte
    • Db.book.find ({$or: [{' BookName ': ' Shuihuzuan '},{' Bookprice ': {$lt: 10000}]} or query
    • Db.book.find ({' BookName ': ' Shuihuzuan ', ' Bookprice ': {$lt: 10000}}) and query
    • Db.book.find (). Sort ({' Bookprice ': 1}) sorting
    • Db.book.find (). Skip (1). Limit (2) paging
    • Db.book.find ({' count ': {$exists: false}}). Count () Determine if the Count field exists
    • Db.foo.find ({name:{$in: [null], $exists: true}}) query for NULL records
        • ! = $ne
  • 5. Modifications
    • Db.Col.Update({ ' title ' :" MongoDB Tutorial '
    • The above statement only modifies the first found document, and if you want to modify multiple identical documents, you need to set the multi parameter to True.
>Db.Col.Update({' Title ':' MongoDB Tutorial '},{$set: {' title ':' MongoDB '}}, {multi:true} ' set: Update field, such as No field increase table structure
    • Db.users.update ({"Name": "LECAF"}, {"Age": 10}) modifies the NAME=LECAF data to age=10, the first parameter is the lookup condition, the second parameter is the modification, except the primary key, the other content is replaced by the contents of the second parameter , the primary key cannot be modified,
    • Db.book.update ({},{$inc: {' Bookprice ': 600}},{multi:true}) price increased by 600

MongoDB General Command

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.