MongoDB (ii) shell command

Source: Internet
Author: User
Tags mongodb

Create a database

The syntax format for MongoDB database creation is as follows:

use DATABASE_NAME

If the database does not exist, create the database, or switch to the specified database.

If you want to see all the databases, you can use the show DBS command:

show dbs
Deleting a database

The syntax format for MongoDB database deletion is as follows:

db.dropDatabase()

Delete the current database, which is test by default, and you can use the DB command to view the current database name.

Delete Collection

The collection delete syntax format is as follows:

db.collection.drop()
Insert Document

The data structure of the document is basically the same as JSON.
All data stored in the collection is in the Bson format.
Bson is a binary form of a class JSON storage format, referred to as binary JSON.

MongoDB uses the Insert () or Save () method to insert a document into the collection, with the following syntax:

db.COLLECTION_NAME.insert(document)

Example:

db.col.insert({"title": "个人主页", "description": "zhongchun的主页", "by": "zhongchun", "url": "http://yuzhongchun.com", "tags": [‘mongodb‘, ‘database‘, ‘NoSQL‘], "likes": 100})
document=({"title": "个人主页", "description": "zhongchun的主页", "by": "zhongchun", "url": "http://yuzhongchun.com", "tags": [‘mongodb‘, ‘database‘, ‘NoSQL‘], likes: 100})// 执行后如下{    "title" : "个人主页",    "description" : "zhongchun的主页",    "by" : "zhongchun",    "url" : "http://yuzhongchun.com",    "tags" : [        "mongodb",        "database",        "NoSQL"    ],    "likes" : 100}// 执行插入操作db.col.insert(document)WriteResult({ "nInserted" : 1 })

You can also use the Db.col.save (document) command to insert documents. If you do not specify the _id field, the Save () method is similar to the Insert () method. If the _id field is specified, the data for the _id is updated.

Update document

MongoDB uses the update () and Save () methods to update the documents in the collection. Next, let's take a look at the application of the two functions and their differences.

Update () method

The syntax format is as follows:

db.collection.update(   <query>,   <update>,   {     upsert: <boolean>,     multi: <boolean>,     writeConcern: <document>   })

Parameter description:

query : update的查询条件,类似sql update查询内where后面的。update : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。writeConcern :可选,抛出异常的级别。

Example:

db.col.update({‘title‘: "个人主页"}, {$set:{‘title‘: "BerMaker"}})
Save () method

The syntax format is as follows:

db.collection.save(   <document>,   {     writeConcern: <document>   })

Parameter description:

document : 文档数据。writeConcern :可选,抛出异常的级别。

Example:

db.col.save({"_id" : ObjectId("59a6a3d4963d665550f6e2d2"), "title" : "不涸", "description": "zhongchun的主页"})

Operation Result:

> db.col.find().pretty(){    "_id" : ObjectId("59a6a3d4963d665550f6e2d2"),    "title" : "不涸",    "description" : "zhongchun的主页"}
Delete Document backup and restore
mongodump -h 10.94.241.53 --port 8017 -d boundary_tool -o /home/map/data/mongo/201708301810
mongorestore -h 10.101.44.169 --port 27017 -d boundary_tool --drop /home/wuzhibin/yuzhongchun/data/mongo/boundary_tool

Drop indicates that all records are deleted before recovery.

Single table export and import

Export

mongoexport --host 10.206.210.30 --port 27019 --db boundary_tool --collection boundary --out boundary.json

Import

mongoimport --host 127.0.0.1 --port 8017 --db boundary_tool --collection boundary --file boundary.json
Reference
    1. MongoDB Database Operations--Backup restore Export Import

MongoDB (ii) shell 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.