MongoDB - Basics


本文旨在给出mongodb的基础使用方法。如果你从来没有用过mongodb,通过这篇文章也能快速地学会创建collection。

MongoDB is a cross-platform document-oriented database program. It is written in C++ and aiming at providing solutions for extensive and high-performance data storage for web applications.

MongoDB is a product between relational DB and non-relational DB, and has the maximum functions among non-relational DB.


Premise

Install MongoDB

Choose the proper version and download Mongo DB from https://www.mongodb.com/download-center#community.

Double click on the mongodb-xxxxxxxxxxxxx-signed.msi file and finish MongoDB setup process.

Here we install mongodb to C:\mongodb.

 

Start MongoDB

Create the following folders:

  • dada under C:\mongodb
  • log under C:\mongodb
  • db under C:\mongodb\data (where the data will be stored in the system)

Open cmd (Ctrl+R) as Administrator, navigate to mongodb\bin folder, run

mongod --directoryperdb --dbpath C:\mongodb\data\db --logpath C:\mongodb\log\mongo.log  --logappend --rest --install

C:\mongodb\bin>mongod --directoryperdb --dbpath C:\mongodb\data\db --logpath C:\mongodb\log\mongo.log  --logappend --rest -- install
 
 
2017-09-21T14:35:03.645+0800 I CONTROL  [main] ** WARNING: --rest is specified without --httpinterface,
2017-09-21T14:35:03.647+0800 I CONTROL  [main] **          enabling http interface

Now we should be able to run the service, run:

net start MongoDB
C:\mongodb\bin>net start MongoDB
The MongoDB service is starting.
The MongoDB service was started successfully.

Now mongodb is running in the background as a service.

Use command mongo to work in the mongo shell:

C:\mongodb\bin>mongo
MongoDB shell version v3.4.9
connecting to: mongodb: //127 .0.0.1:27017
MongoDB server version: 3.4.9
Server has startup warnings:
2017-09-21T14:41:58.119+0800 I CONTROL  [main] ** WARNING: --rest is specified without --httpinterface,
2017-09-21T14:41:58.121+0800 I CONTROL  [main] **          enabling http interface
2017-09-21T14:41:58.658+0800 I CONTROL  [initandlisten]
2017-09-21T14:41:58.658+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled  for  the database.
2017-09-21T14:41:58.659+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-09-21T14:41:58.659+0800 I CONTROL  [initandlisten]
>


Syntax

To create a database and switch to it:

>use mycustomers
switched to db mycustomers
>

 Check the database:

>db
mycustomers
>

Create one user for this db:

> db.createUser({user: "brad" , pwd : "1234" ,roles:[ "readWrite" , "dbAdmin" ]});
Successfully added user: {  "user"  "brad" "roles"  : [  "readWrite" "dbAdmin"  ] }


Collections are very similar to tables in relational DB. Create a collection:

> db.createCollection( 'customers' );
"ok"  : 1 }


show all of the collection in the DB: 

> show collections
customers


Insert a document to this document:

> db.customers.insert({first_name: "John" ,last_name: "Doe" });
WriteResult({  "nInserted"  : 1 })


To see the documents in the collection:

> db.customers. find ();
"_id"  : ObjectId( "59c4c65b136288e2ae5a4aca" ),  "first_name"  "John" "last_name"  "Doe"  }

 Note: ObjectId: unique ID to identify the document

 

Add more data to this collection:

> db.customers.insert([{first_name: "Steven" ,last_name: "Smith" },{first_name: "Joan" ,last_name: "Johnson" ,gender: "female" }]);
BulkWriteResult({
         "writeErrors"  : [ ],
         "writeConcernErrors"  : [ ],
         "nInserted"  : 2,
         "nUpserted"  : 0,
         "nMatched"  : 0,
         "nModified"  : 0,
         "nRemoved"  : 0,
         "upserted"  : [ ]
})

Note: for customer "Joan" we have one more filed of "gender".

Use command

db.customers.find().pretty(); 

to make the message nicer to see.

> db.customers. find ().pretty();
 
{
         "_id"  : ObjectId( "59c4c65b136288e2ae5a4aca" ),
         "first_name"  "John" ,
         "last_name"  "Doe"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acb" ),
         "first_name"  "Steven" ,
         "last_name"  "Smith"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acc" ),
         "first_name"  "Joan" ,
         "last_name"  "Johnson" ,
         "gender"  "female"
}

 

To add a "gender" field for customer John:

> db.customers.update({first_name: "John" },{first_name: "John" ,last_name: "Doe" ,gender: "male" });
WriteResult({  "nMatched"  : 1,  "nUpserted"  : 0,  "nModified"  : 1 })

Check the latest table:

> db.customers. find ().pretty();
{
         "_id"  : ObjectId( "59c4c65b136288e2ae5a4aca" ),
         "first_name"  "John" ,
         "last_name"  "Doe" ,
         "gender"  "male"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acb" ),
         "first_name"  "Steven" ,
         "last_name"  "Smith"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acc" ),
         "first_name"  "Joan" ,
         "last_name"  "Johnson" ,
         "gender"  "female"
}

 

Update Steven's  gender

> db.customers.update({first_name: "Steven" },{$ set :{gender: "male" }});
WriteResult({  "nMatched"  : 1,  "nUpserted"  : 0,  "nModified"  : 1 })

 

Add "age" to Steven

> db.customers.update({first_name: "Steven" },{$ set :{age:25}});
WriteResult({  "nMatched"  : 1,  "nUpserted"  : 0,  "nModified"  : 1 })
> db.customers. find ().pretty();
 
{
         "_id"  : ObjectId( "59c4c65b136288e2ae5a4aca" ),
         "first_name"  "John" ,
         "last_name"  "Doe" ,
         "gender"  "male"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acb" ),
         "first_name"  "Steven" ,
         "last_name"  "Smith" ,
         "gender"  "male" ,
         "age"  : 25
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acc" ),
         "first_name"  "Joan" ,
         "last_name"  "Johnson" ,
         "gender"  "female"

 

Increase age  of Steven

//Add  age to Steven
> db.customers.update({first_name: "Steven" },{$inc:{age:5}});
WriteResult({  "nMatched"  : 1,  "nUpserted"  : 0,  "nModified"  : 1 })
> db.customers. find ().pretty();
{
         "_id"  : ObjectId( "59c4c65b136288e2ae5a4aca" ),
         "first_name"  "John" ,
         "last_name"  "Doe" ,
         "gender"  "male"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acb" ),
         "first_name"  "Steven" ,
         "last_name"  "Smith" ,
         "gender"  "male" ,
         "age"  : 30
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acc" ),
         "first_name"  "Joan" ,
         "last_name"  "Johnson" ,
         "gender"  "female"
}


Unset Steven's age

> db.customers.update({first_name: "Steven" },{$ unset :{age:1}});
WriteResult({  "nMatched"  : 1,  "nUpserted"  : 0,  "nModified"  : 1 })
> db.customers. find ().pretty();
{
         "_id"  : ObjectId( "59c4c65b136288e2ae5a4aca" ),
         "first_name"  "John" ,
         "last_name"  "Doe" ,
         "gender"  "male"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acb" ),
         "first_name"  "Steven" ,
         "last_name"  "Smith" ,
         "gender"  "male"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acc" ),
         "first_name"  "Joan" ,
         "last_name"  "Johnson" ,
         "gender"  "female"
}

 

update an nonexistent item

> db.customers.update({first_name: "Mary" },{first_name: "Mary" ,last_name: "Samson" });
WriteResult({  "nMatched"  : 0,  "nUpserted"  : 0,  "nModified"  : 0 })

 

Upsert an item

> db.customers.update({first_name: "Mary" },{first_name: "Mary" ,last_name: "Samson" },{upsert: true });
WriteResult({
         "nMatched"  : 0,
         "nUpserted"  : 1,
         "nModified"  : 0,
         "_id"  : ObjectId( "59c4ce088dad8653d99a67f1" )
})
> db.customers. find ().pretty();
{
         "_id"  : ObjectId( "59c4c65b136288e2ae5a4aca" ),
         "first_name"  "John" ,
         "last_name"  "Doe" ,
         "gender"  "male"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acb" ),
         "first_name"  "Steven" ,
         "last_name"  "Smith" ,
         "gender"  "male"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acc" ),
         "first_name"  "Joan" ,
         "last_name"  "Johnson" ,
         "gender"  "female"
}
{
         "_id"  : ObjectId( "59c4ce088dad8653d99a67f1" ),
         "first_name"  "Mary" ,
         "last_name"  "Samson"
}

 

Update “gender” to "sex" for Steven

> db.customers.update({first_name: "Steven" },{$rename:{ "gender" : "sex" }})
WriteResult({  "nMatched"  : 1,  "nUpserted"  : 0,  "nModified"  : 1 })
> db.customers. find ().pretty();
{
         "_id"  : ObjectId( "59c4c65b136288e2ae5a4aca" ),
         "first_name"  "John" ,
         "last_name"  "Doe" ,
         "gender"  "male"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acb" ),
         "first_name"  "Steven" ,
         "last_name"  "Smith" ,
         "sex"  "male"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acc" ),
         "first_name"  "Joan" ,
         "last_name"  "Johnson" ,
         "gender"  "female"
}
{
         "_id"  : ObjectId( "59c4ce088dad8653d99a67f1" ),
         "first_name"  "Mary" ,
         "last_name"  "Samson"
}

 

remove Steven

> db.customers.remove({first_name: "Steven" })
WriteResult({  "nRemoved"  : 1 })
> db.customers. find ().pretty();
{
         "_id"  : ObjectId( "59c4c65b136288e2ae5a4aca" ),
         "first_name"  "John" ,
         "last_name"  "Doe" ,
         "gender"  "male"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acc" ),
         "first_name"  "Joan" ,
         "last_name"  "Johnson" ,
         "gender"  "female"
}
{
         "_id"  : ObjectId( "59c4ce088dad8653d99a67f1" ),
         "first_name"  "Mary" ,
         "last_name"  "Samson"
}

 

Just delete the first one with the first_name "Steven"

> db.customers.remove({first_name: "Steven" },{justOne: true })

 

Find messages according to specific condition

> db.customers. find ({first_name: "Joan" });
 
"_id"  : ObjectId( "59c4c7fe136288e2ae5a4acc" ),  "first_name"  "Joan" "last_name"  "Johnson" "gender"  "female"  }

 

Sort

> db.customers. find (). sort ({last_name:1}).pretty()
{
         "_id"  : ObjectId( "59c4c65b136288e2ae5a4aca" ),
         "first_name"  "John" ,
         "last_name"  "Doe" ,
         "gender"  "male"
}
{
         "_id"  : ObjectId( "59c4c7fe136288e2ae5a4acc" ),
         "first_name"  "Joan" ,
         "last_name"  "Johnson" ,
         "gender"  "female"
}
{
         "_id"  : ObjectId( "59c4ce088dad8653d99a67f1" ),
         "first_name"  "Mary" ,
         "last_name"  "Samson"
}

 

 

Count:

> db.customers. find ().count()
3
 
> db.customers. find ({gender: "male" }).count()
1


Limit the first N messages:

> db.customers. find ().limit(2)
"_id"  : ObjectId( "59c4c65b136288e2ae5a4aca" ),  "first_name"  "John" "last_name"  "Doe" "gender"  "male"  }
"_id"  : ObjectId( "59c4c7fe136288e2ae5a4acc" ),  "first_name"  "Joan" "last_name"  "Johnson" "gender"  "female"  }

 

forEach

> db.customers. find ().forEach( function (doc){print( "Customer name: " +doc.first_name)})
Customer name: John
Customer name: Joan
Customer name: Mary

 

Mango Management Studio

Mongo Management Studio is the best way to work with MongoDB the easy way. Because of the clean and light user interface, you can execute the typical MongoDB commands fast and effective, without using the MongoDB shell. It is suitable to assist your local development and test process as well as working with your Mongo databases on remote or production servers. By using a basic but strict user and rights management you have full control of the access rights of every single user. Of course you are also able to utilize the integrated user management of MongoDB within Mongo Management Studio.

Connect to MongoDB by host name and port:

You can get your host from MongoDB shell by:

 

  > getHostName()
AIQIAN-CN

Give the server connection by format: AIQIAN_CN:27017. 27017 is the default port for MongoDB.

Save and connect.

You can see the collection "customers" created just now. In the table view, we can view the collection as a "table" with several columns.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值