MongoDB basic commands and curd operations

Source: Internet
Author: User
Tags mongodb

One, the basic command:

1. View all databases: Show DBS

2, select database Use DbName, if the database does not exist, the database will be created

3. View all database related operations (similar to JavaScript functions) Db.help ()

Use Db.help () to get all of the database operations 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 a 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 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 at 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 the DB commands
Db.loadserverscripts () loads all the scripts in Db.system.js
Db.logout ()
Db.printcollectionstats () displays all collection information
Db.printreplicationinfo ()
Db.printshardingstatus () Viewing shard 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 () Viewing 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 inserting an object into a nonexistent collection, a new collection object can also be created using Db.createcollection ("users")

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

8, Display collection all objects: Db.collectionName.find ()

9, Delete collection all objects: Db.collectionName.remove () just deletes all the data in the collection, but the collection and the corresponding index are still present

Curd related operation of two mongdb

1, adding operations

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, bulk Add (using JavaScript for loop add):

> 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 _id already exists with insert (), the information will be modified if you use Save ()

> Db.users.find ()

{"_id": 123, "name": "123"} Pre-Modified value

{"_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 repeat 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 modified by the Save () method

>

2, delete operation

2.1: Conditional 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})//delete 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.