MongoDB Database: Basics

Source: Internet
Author: User
Tags mongodb

Three elements: Database collection document (extended Bson of JSON)

Service start restart stop: sudo service MongoDB start (Stop,restart)

Modifying a configuration file/etc/mongodb.conf
Add Smallfiles=true

View current Database db

View all databases Show DBS

View database Information Db.stats ()

Switch database use database name (not just point to this database, but not create, insert data when creating collection)

Delete current point to Database Db.dropdatabase ()

Create collection db.createcollection (' name ', {opt})

Create a fixed collection (Size size unit Kb,max document maximum number of bars):d b.createcollection (' collection name ', {capped:true,size:5,max:5})

View Collection Show Collections

Delete Collection db. Collection name. Drop ()

Insert Data (the collection is automatically created): Db. Collection name. Insert ({Document 1, document 2,...})

Simple query: DB. Collection name. Find ()

Modify a document (without multi modify one, multi:true modify multiple) db.update ({condition},{modified content},{multi:true})

Modify some key values ($set) in a document Db.update ({},{$set: {key: ' Value '}})

Save: DB. Collection name. Save ()

Delete (justone:true delete one, the default number of bars): Db. Collection name. Remove ({condition},{justone: Default false})

The query returns the first: DB. Collection name. FindOne ({condition})
Result formatting: DB. Collection name. Find ({}). Pretty ()

Comparison operators:
equals: Default is equal to (query document with Title 3) Db.sub.find ({title:3})

Less than ($lt) query (title less than 5) db.sub.find ({title:{$lt: 5}})

Greater than ($GT) query (title greater than 5) Db.sub.find ({title:{$gt: 5}})

There is greater than or equal to: $gte, less than or equal to: $lte, not equal to: $ne

Logical operator with (comma) such as query name is XX, gender is true: Db.stu.find ({name: ' xx ', gender:true})

or ($or []) query name is XX or xxx: Db.stu.find ({$or: [{name: ' xx '},{name: ' xxx '}]})

The range operator ($in: [], $nin: []): If the query age is 18-29: Db.stu.find ({age:{$in: [18,29]}})

Regular expressions (in//or $regex): Query the King's name: Db.find ({name:/^/})

Custom functions ($where + functions) such as query age greater than 20: Db.stu.find ({$where: function () {return this.age<20}})

Limit (how many clauses are obtained) such as Get 10: db. Collection. Find (). Limit (10)

Skip (skip a few) such as Skip 2: Db.jihe.find (). Skip (2)

Projection (Displays the specified field display setting 1, does not display the Settings 0_id column default display): Displays name and age Db.find ({condition},{name:1,age:1})

Sort (sort, with a value of 1 ascending, a value of 1 descending):d B. Collection. Find (). Sort ({field: 1,....})

Statistics (count ()) db. Collection. Count ({condition})

De-weight (distinct ()): db. Set. Distinct (' Redo field ', {condition})

MongoDB Database: Basics

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.