mongodb增删改查

插入数据

插入数据有两种方法: db.collection_name.insert({})或者db.collection_name.save({})
使用insert插入数据:

1
2
3
4
> db.createCollection('stu')
{ "ok" : 1 }
> db.stu.insert({name:'zhao', age:18})
WriteResult({ "nInserted" : 1 })

不能插入_id相同的数据,否则将会报错

1
2
3
4
5
6
7
8
> db.stu.insert({_id:ObjectId('5ccbcff1003d4e50497243e7'), name:'zhao', age:18})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: test.stu index: _id_ dup key: { : ObjectId('5ccbcff1003d4e50497243e7') }"
}
})

可以使用save()函数替代

1
2
> db.stu.save({_id:ObjectId('5ccbcff1003d4e50497243e7'), name:'zhao', age:18})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

使用save()函数时,如果不指定 _id 字段 save() 方法类似于 insert() 方法。如果指定 _id 字段,则会更新该 _id 的数据。

更新数据

语法如下:

1
2
3
4
5
6
7
8
9
db.collection_name.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)

  • query: update的查询提交,类似sql的where后面的语句
  • update: update的对象和一些更新的操作符
  • upsert: 可选,这个意思是,如果要更新的数据不存在时,是否需要插入数据,true表示需要插入,默认时false,不插入.
  • multi: 可选,默认是false,只更新找到的第一条数据,如果这个值为true,则会更新所有找到的数据
  • writeConcern: 可选,抛出异常的级别

更新一条数据:
使用pretty可以更好的阅读

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
> db.stu.find().pretty()
{
"_id" : ObjectId("5ccbcff1003d4e50497243e7"),
"name" : "zhao",
"age" : 18
}
{
"_id" : ObjectId("5ccbd01a003d4e50497243e8"),
"name" : "zhao",
"age" : 18
}
> db.stu.update({name:'zhao'}, {$set:{name:'qian'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.stu.find().pretty()
{
"_id" : ObjectId("5ccbcff1003d4e50497243e7"),
"name" : "qian",
"age" : 18
}
{
"_id" : ObjectId("5ccbd01a003d4e50497243e8"),
"name" : "zhao",
"age" : 18
}

更新所有的数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
> db.stu.update({age:18}, {$set:{age:'20'}}, {multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.stu.find().pretty()
{
"_id" : ObjectId("5ccbcff1003d4e50497243e7"),
"name" : "sun",
"age" : "20"
}
{
"_id" : ObjectId("5ccbd01a003d4e50497243e8"),
"name" : "sun",
"age" : "20"
}
>

删除数据

语法:

1
2
3
4
5
6
7
db.collection_name.remove(
大专栏  mongodb增删改查e"> <query>,
{
justOne: <boolean>,
writeConcern: <document>
}
)

  • query: (可选)删除数据的条件
  • justOne: (可选)如果值为1或者true,则表示删除所有,默认为false,删除全部符合条件的数据
  • writeConcern: (可选)抛出异常的级别
    或者可以使用db.collection_name.deleteOne()db.collection_name.deleteMany()函数来起到删除的作用.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    > db.stu.find()
    { "_id" : ObjectId("5ccd3ad3a2e1d0b0ae08f961"), "name" : "zhao", "age" : 18 }
    { "_id" : ObjectId("5ccd3adea2e1d0b0ae08f962"), "name" : "qian", "age" : 20 }
    { "_id" : ObjectId("5ccd3aeba2e1d0b0ae08f963"), "name" : "sun", "age" : 32 }

    > db.stu.remove({name:'zhao'})
    WriteResult({ "nRemoved" : 1 })
    > db.stu.find()
    { "_id" : ObjectId("5ccd3adea2e1d0b0ae08f962"), "name" : "qian", "age" : 20 }
    { "_id" : ObjectId("5ccd3aeba2e1d0b0ae08f963"), "name" : "sun", "age" : 32 }

    > db.stu.deleteOne({name:'qian'})
    { "acknowledged" : true, "deletedCount" : 1 }
    > db.stu.find()
    { "_id" : ObjectId("5ccd3aeba2e1d0b0ae08f963"), "name" : "sun", "age" : 32 }
    > db.stu.deleteMany({name:'sun'})
    { "acknowledged" : true, "deletedCount" : 1 }
    > db.stu.find()
    >

remove() 方法 并不会真正释放空间。
需要继续执行 db.repairDatabase() 来回收磁盘空间。

1
2
3
> db.repairDatabase()
或者
> db.runCommand({ repairDatabase: 1 })

查找数据

MongoDB 查询文档使用 find()方法。
find() 方法以非结构化的方式来显示所有文档。

db.collection_name.find(query, projection)

  • query: 可选,使用查询操作符指定查询条件
  • projection: 可选,使用投影操作符指定返回的键.如果省略, 默认查询时返回文档的所有键值.
    db.collection_name.find().pretty()
    以易读 的 形式返回数据.

AND条件

在find中传入多个值,以逗号隔开,就是AND查询了
db.collection_name.find({key1:value1, key2:value2}).pretty()

OR条件

OR关键字语法 如下:

1
2
3
4
5
6
7
>db.collection_name.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()

OR和AND连用

OR语句中,要用[],
AND中要用 , 隔开
find 中要是一个 {}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> db.stu.find().pretty()
{
"_id" : ObjectId("5ccd425ca2e1d0b0ae08f964"),
"name" : "zhao",
"age" : 18
}
{
"_id" : ObjectId("5ccd4264a2e1d0b0ae08f965"),
"name" : "qian",
"age" : 20
}
{ "_id" : ObjectId("5ccd426ca2e1d0b0ae08f966"), "name" : "sun", "age" : 22 }
{ "_id" : ObjectId("5ccd4273a2e1d0b0ae08f967"), "name" : "li", "age" : 30 }
> db.stu.find({age:{$gte:20}, $or:[{name:'qian'}, {name:'sun'}]})
{ "_id" : ObjectId("5ccd4264a2e1d0b0ae08f965"), "name" : "qian", "age" : 20 }
{ "_id" : ObjectId("5ccd426ca2e1d0b0ae08f966"), "name" : "sun", "age" : 22 }

条件操作符

上例中的{age:{$gt:20}}的$gt就是操作符,指的时大于,类似的还有以下:

$gt ——– greater than >

$gte ——— gt equal >=

$lt ——– less than <

$lte ——— lt equal <=

$ne ———– not equal !=

$eq ——– equal =

limit()函数

使用limit()函数可以限制读取的数量,该函数接受一个数字参数.
db.collection_name.find().limit(20)

skip()函数

使用skip()函数可以用来跳过指定数量的数据,也时接收一个数字参数
db.collection_name.find().skip(20)

或者也可以结合Limit()使用
db.collection_name.find().skip(2).limit(2)

原文地址:https://www.cnblogs.com/lijianming180/p/12026742.html