51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1038|回复: 0
打印 上一主题 下一主题

MongoDB Installation/Basic

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2018-4-29 18:17:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Mongo db
Installation
1)Download installer from mongo website and run installer
2)Create data directory under disk’s root directory, d:/data/db;

Start mongo db server
Go to installation bin directory, and run command: mongod --dbpath d:/data/db

Database operation
Create database : use DATABASE_NAME
If the database does not exist, then create this database, otherwise, switch to this specific database.
Example
>use runoob
Switched to db runoob
>db
Runoob
>

>show dbs
Local 0.078DG
Admin 0.078G

Create a collection
New database runoob is not shown by using command “show dbs” , need to insert data into this new created database.
>db.runoob.insert ({“name”:”mongodbDEmo”})
WriteResult({“nInserted:1})
>show dbs
Local 0.078G
Admin 0.078 G
Runoob 0.078G

Delete a collection db.runoob.drop()


Delete a database :db.dropDatabase() , delete current database.
>show dbs
Local 0.078G
Admin 0.078G
Runoob 0.078G
>use runoob
Switched to db runoob
>
>db.dropDatabase()
{“dropped”:”runoob”,”ok”:1}



Find all records
Db.user.find() //user is collection name.

Insert a record
Db.user.insert ({“name”:”Jacky”})

Update a record //add a field and update name data.
Db.user.update({“name”:”Jacky”},{“name”:”Jacky tt”,”age”:”30”})

Remove a record
Db.user.remove({“name”:”Jacky”})

Add a field
db.users.update({},{$set:{gender:""}},{multi:1})
Remove a field
db.users.update({},{$unset:{gender:""}},false,true)

Add embedded records
db.multi.insert({"empId":1,"members":[{"name":"Blue1","age":"23","gender":"male"},{"name":"Blue2","age":"33","gender":"female"},{"name":"Blue3","age":"43","gender":"male"}]})

Query embedded records
db.multi.find({"members":{"$elemMatch":{"name":"Blue1","age":"23"}}})
db.multi.find({"empId":1.0})

Linked query
Collection A: users [id, name,email]
Collection B posts[id, author,postId]

author=db.users.findOne({"name":"cindy"})
for (var post=db.posts.find({"author":author.name});post.hasNext();) {
printjson(post.next().postId)};


Aggregation
Filter some fields in query process
db.multi.aggregate({$project:{empId:1}}), get ID and empId only.
db.multi.aggregate({$project:{_id:0,members:1}}) get members field only.

Unwind: split array fields to multiple individual records

db.multi.aggregate({$project:{_id:0,members:1}},{$unwind:"$members"})
var test=db.multi.aggregate({$project:{_id:0,members:1}},{$unwind:"$members"})
for (;test.hasNext();) {
printjson(test.next().members.name)};


test=db.multi.findOne()
printjson(test.members[0].name);
Multi-embedding documents

Auth management

db.createUser({user:"admin",customData:{description:"superuser"},pwd:"admin",roles:[{role:"root",db:"admin"}]})

Show users

Start mongo db server with auth info.
Go to installation bin directory, and run command: mongod --dbpath d:/data/db -auth
db.auth("admin","admin")

Use testdb, then query all records.





分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-9-22 12:37 , Processed in 0.070503 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表