MongoDB basic commands and curd operations

Source: Internet
Author: User
Tags mongodb

One, basic command:

1, view all databases: Show DBS

2, select database Use dbname, if the database does not exist, you will create a new database

3, view all database-related operations (like JavaScript functions) Db.help ()

Use Db.help () to get all the operations of the database as follows: > Db.help ()
DB methods:
Db.adduser (UserDocument)
Db.admincommand (nameordocument)-Switches to ' admin ' db, and runs command [just calls DB.R
Uncommand (...)]
Db.auth (username, password)
Db.clonedatabase (fromhost) Copy Database
DB.COMMANDHELP (name) returns the Help for the command
Db.copydatabase (Fromdb, Todb, Fromhost)
Db.createcollection (name, {size: ..., capped: ..., max: ...}) Create collection
Db.currentop () displays currently executing operations in the DB
Db.dropdatabase () Drop database
Db.eval (func, args) run code server-side
Db.fsynclock () flush data to disk and lock server for backups
Db.fsyncunlock () unlocks server following a Db.fsynclock ()
Db.getcollection (CNAME) Same as db[' cname '] or db.cname
Db.getcollectionnames () Gets the names of all the collections in the database
Db.getlasterror ()-Just returns the Err Msg string
Db.getlasterrorobj ()-Return to full status object
Db.getmongo () Get the Server connection object
Db.getmongo (). Setslaveok () Allow queries on a replication slave server
Db.getname () get the database name
Db.getpreverror ()
Db.getprofilinglevel ()-Deprecated
Db.getprofilingstatus ()-Returns if profiling is on and slow threshold
Db.getreplicationinfo ()
DB.GETSIBLINGDB (name) get the DB in the same server as this one
Db.hostinfo () Get details about the server ' s host
Db.ismaster () Check replica primary status
Db.killop (Opid) kills the current operation in the DB
Db.listcommands () Lists all DB commands
Db.loadserverscripts () Loads the scripts in Db.system.js
Db.logout ()
Db.printcollectionstats () displays all collection information
Db.printreplicationinfo ()
Db.printshardingstatus () View fragmentation information
Db.printslavereplicationinfo ()
Db.removeuser (username)
Db.repairdatabase ()
Db.reseterror ()
Db.runcommand (cmdobj) run a database command. If Cmdobj is a string, turns it into {cmdobj
: 1}
Db.serverstatus () View server information
Db.setprofilinglevel (level,<slowms>) 0=off 1=slow 2=all
Db.setverboseshell (flag) display extra information in shell output
Db.shutdownserver ()
Db.stats () View database-related information
Db.version () Current version of the server
>


4, delete database Db.dropdatabase ()

5, view all collections show collections

6, new collection Db.collectionName.insert ({_id:123}) when you insert an object into a nonexistent collection, a new collection object can also be created by using Db.createcollection ("users")

7, delete collection Db.collectionName.drop () Delete collection

8, display all objects of the collection: Db.collectionName.find ()

9, delete all objects of the collection: Db.collectionName.remove () only deletes all the data in the collection, but the collection and corresponding indexes are still present

Curd related operation of two mongdb

1, add action

1.1, Single add:

> Use Mogo
Switched to DB Mogo
> Db.users.insert ({_id:123,name: "123"})
> Db.users.find ()
{"_id": 123, "name": "123"}

Switched to DB Mogo

> Db.users.insert ({_id:123,name: "123"})

> Db.users.find ()

{"_id": 123, "name": "123"}

Switched to DB Mogo

> Db.users.insert ({_id:123,name: "123"})

> Db.users.find ()

{"_id": 123, "name": "123"}

Switched to DB Mogo

> Db.users.insert ({_id:123,name: "123"})

> Db.users.find ()

{"_id": 123, "name": "123"}

1.2, add in bulk (add with JavaScript for loop):

> for (var i=0;i<5;i++) {

... Db.users.insert ({

... _id:i,name: "Name" +i,sex:20+i,address: "Address" +i,tell: "134567890" +i}

... )}

> Db.users.find ()

{"_id": 123, "name": "123"}

{"_id": 0, "name": "NAME0", "Sex": "Address": "Address0", "Tell": "1345678900"}

{"_id": 1, "name": "Name1", "Sex": "Address": "Address1", "Tell": "1345678901"}

{"_id": 2, "name": "Name2", "Sex": "Address": "Address2", "Tell": "1345678902"}

{"_id": 3, "name": "Name3", "Sex": "Address": "ADDRESS3", "Tell": "1345678903"}

{"_id": 4, "name": "Name4", "Sex": "Address": "Address4", "Tell": "1345678904"}

>

1.3,insert () and save (): If the _id already exists when inserting with insert (), and if you use Save (), the information is modified

> Db.users.find ()

{"_id": 123, "name": "123"} Value before modification

{"_id": 0, "name": "NAME0", "Sex": "Address": "Address0", "Tell": "1345678900"}

{"_id": 1, "name": "Name1", "Sex": "Address": "Address1", "Tell": "1345678901"}

{"_id": 2, "name": "Name2", "Sex": "Address": "Address2", "Tell": "1345678902"}

{"_id": 3, "name": "Name3", "Sex": "Address": "ADDRESS3", "Tell": "1345678903"}

{"_id": 4, "name": "Name4", "Sex": "Address": "Address4", "Tell": "1345678904"}

> Db.users.insert ({_id:123,name: "12345"})

E11000 duplicate key error index:mogo.users.$_id_ dup key: {: 123.0} primary key duplicate error

> Db.users.save ({_id:123,name: "12345"})

> Db.users.find ()

{"_id": 0, "name": "NAME0", "Sex": "Address": "Address0", "Tell": "1345678900"}

{"_id": 1, "name": "Name1", "Sex": "Address": "Address1", "Tell": "1345678901"}

{"_id": 2, "name": "Name2", "Sex": "Address": "Address2", "Tell": "1345678902"}

{"_id": 3, "name": "Name3", "Sex": "Address": "ADDRESS3", "Tell": "1345678903"}

{"_id": 4, "name": "Name4", "Sex": "Address": "Address4", "Tell": "1345678904"}

{"_id": 123, "name": "12345"} The value after the modification of the Save () method

>

2, delete operation

2.1: Condition Delete db.collectionNmae.remove ({}) {} put delete condition

> Db.users.find ()

{"_id": 0, "name": "NAME0", "Sex": "Address": "Address0", "Tell": "1345678900"}

{"_id": 1, "name": "Name1", "Sex": "Address": "Address1", "Tell": "1345678901"}

{"_id": 2, "name": "Name2", "Sex": "Address": "Address2", "Tell": "1345678902"}

{"_id": 3, "name": "Name3", "Sex": "Address": "ADDRESS3", "Tell": "1345678903"}

{"_id": 4, "name": "Name4", "Sex": "Address": "Address4", "Tell": "1345678904"}

{"_id": 123, "Age": "12345"}

> Db.users.remove ({_id:123})//deleted by ID

> Db.users.find ()

{"_id": 0, "name": "NAME0", "Sex": "Address": "Address0", "Tell": "1345678900"}

{"_id": 1, "name": "Name1", "Sex": "Address": "Address1", "Tell": "1345678901"}

{"_id": 2, "name": "Name2", "Sex": "Address": "Address2", "Tell": "1345678902"}

{"_id": 3, "name": "Name3", "Sex": "Address": "ADDRESS3", "Tell": "1345678903"}

{"_id": 4, "name": "Name4", "Sex": "Address": "Address4", "Tell": "1345678904"}

>

2.2: Delete all Db.collectionName.remove ()


3, modify the operation

3.1:update Operation Db.collectionName.update ({Modify Condition},{modified value})

> Db.users.find ()

{"_id": 0, "name": "NAME0", "Sex": "Address": "Address0", "Tell": "1345678900"}

{"_id": 1, "name": "Name1", "Sex": "Address": "Address1", "Tell": "1345678901"}

{"_id": 2, "name": "Name2", "Sex": "Address": "Address2", "Tell": "1345678902"}

{"_id": 3, "name": "Name3", "Sex": "Address": "ADDRESS3", "Tell": "1345678903"}

{"_id": 4, "name": "Name4", "Sex": "Address": "Address4", "Tell": "1345678904"}

> Db.users.update (

... {name: ' NAME0 '},//Modify condition

... {Name: "name00"}) The modified value

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.