MongoDB simple command

1. download free Mongodb from https://www.mongodb.com/download-center#community,  I use windows version

2. install mongodb

3. start mongodb

D:\dev\MongoDB\bin>mongod

4. use command line

D:\dev\MongoDB\bin>mongo

5. list current databases

>show dbs

>show databases

6. create a database named 'lyexnode'

use  lyexnode

7. list tables

>show tables

>show collections

8. list users

>show users

9. list roles

>show roles

https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#mongo-shell-javascript-file

10. execute users.js file

db = connect("localhost:27017/lyexnode");
cursor = db.users.find();
while ( cursor.hasNext() ) {
   printjson( cursor.next() );
}

D:\dev\MongoDB\bin>mongo localhost:27017/lyexnode users.js

D:\dev\MongoDB\bin>mongo users.js

>load("users.js")


11. find data in table/document

a. query, return all of the columns

db.users.findOne({"name":"admin"}) 


b. query with specified return columns. "_id" is returned by default. It has to  be specified as 0 explicitly

db.users.findOne({"name":"admin"},{_id:0, name:1, password:1})


12. insert/update data to document

db.userInfo.insertOne(
   {
      name: "sue",
      age: 26,
      status: "pending"
   }
)
db.userInfo.insert(
   {
      name: "sue",
      age: 26,
      status: "A"
   }
)
db.userInfo.insertMany(
   [
      { name: "sue", age: 26, status: "pending" },
      { name: "bob", age: 25, status: "enrolled" },
      { name: "ann", age: 28, status: "enrolled" }
   ]
)
db.userInfo.updateOne(
   { age: { $lt: 18 } },
   { $set: { status: "reject" } }
)

db.userInfo.updateMany(
   { age: { $lt: 18 } },
   { $set: { status: "reject" } }
)

db.userInfo.replaceOne(
   { name: "sue" },
   { name: "amy", age : 25, score: "enrolled" }
)


13.delete

db.userInfo.deleteOne(
   { status: "reject" }
)
db.userInfo.deleteMany(
   { status: "reject" }
)
db.userInfo.remove(
   { status: "D" }
)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值