MongoDB Preliminary Shell usage notes

Source: Internet
Author: User

I. Data storage structure

MongoDB storage data is divided into 3 layers.

Dbs--collections--documents.

At the bottom is documents, which stores data in JSON format. Collections is a dynamic collection of files that can be understood as a drawer for storing files. DBS is a database that can be understood as a bookcase composed of multiple drawers.

Second, CRUD (creat, read, Updata, delete) operations

1. Create

The Db.collection.insert () method is generally used.

The following is a similar picture is official online, do very intuitive.

  

users is a collection of data to insert, and is created automatically if not defined. Can view collections with show collections. Show collections is a shell helper function, equivalent to Db.getcollectionnames ().

If you do not define the "_id" key after inserting it, it will be created automatically.

2. Read

Generally use the Db.collection.find () method or Db.collection.findOne ().

  

Query criteria is a search condition.

"$lt", "$lte", "$gt", "$gte" correspond to < respectively, <=, >=.

The or query has two methods, $in, $or. When a key needs to match multiple values, use the former, and use the latter when matching multiple keys.

Db.users.find ({users_id:{$in: [12345, "Joe"]}})

Db.restaurants.find (   "cuisine": "Italian"}, {"Address.zipcode": "10075" }]})

And $not, $and, querying arrays, etc.

Projection is the projection, which is the meaning of the return setting. If set to 1 then the specified return, set to 0 is rejected and not returned. "_ID" is returned by default.

Cursor modifier has three methods, limit (), skip (), sort (). The limits, the ignore, the sorting methods, respectively.

3. Update

The db.collection.update () method is generally used.

  

Update criteria is a condition for updating.

Update action is a method of updating behavior, $set, $inc, $push, and so on.

Update option is an upgrade.

4. Delete

The Db.collection.remove () method is generally used.

  

Using the drop () method is faster if you want to delete the entire collection.

  

MongoDB Preliminary Shell usage notes

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.