您的位置:首页 > 数据库 > Mongodb

mongodb的CURD操作简单整理

2016-08-09 16:25 417 查看
最近有机会接触了mongodb这个NoSQL数据库,这里简单整理一下mongodb的增删改查

1. insert方法

1.1 insertOne

db.users.insertOne({
{
name: "sue",
age: 19,
status: "P"
}
})


插入成功返回数据

{
"acknowledged" : true,
"insertedId" :ObjectId("5742045ecacf0ba0c3fa82b0")
}


查找该条记录

db.users.find( { _id: ObjectId("5742045ecacf0ba0c3fa82b0") } )


1.2 insertMany

db.users.insertMany(
[
{ name: "bob", age: 42, status: "A", },
{ name: "ahn", age: 22, status: "A", },
{ name: "xi", age: 34, status: "D", }
]
)


返回数据如
insertOne


2. query 查询常用方法

2.1 查询的语法

db.collection.find( <query filter>, <projection> )


<query filter>
: 查询的条件,语法
{<filed1>:<value1>,...}


条件语法

{ <field1>: { <operator1>: <value1> }, ... }
filed1 :条件字段
operator1:条件操作符, 该项为可选项,默认相等操作
value1:条件值


<projection>
:返回数据的字段

{field1:1, field2:0}
-- 返回field1字段,不返回field2字段
-- _id字段如果不明确指定为0,则默认返回


2.2 查询基本语法

e.g.

查询所有记录

db.users.find({})
db.users.find()


以上两条的结果是一样的,并无区别

-- 查询age等于30的记录
db.users.find({"age":30})
-- in查询,查询status是P D的记录
db.users.find({"status":{$in:['P','D']}})


2.3 多条件查询

2.3.1 and条件查询

e.g.

-- 查询 status等于A 并且 age小于30的记录
db.users.find( { status: "A", age: { $lt: 30 } } )

-- 查询  "2016-08-05" < createTime  < '2016-08-07'
db.collections.find({"createTime":{$gt:"2016-08-05"}, "createTime":{$lt:"2016-08-07"}})


也可以是用
$and
操作符,不过
$and
书写比较复杂,非必须可以不使用
$and


针对操作符有统一的整理内容。

2.3.2 or条件查询

语法略显复杂(使用
$and
操作符进行and条件查询时,语法类似) 如下:

-- 查询status等于A,或者 age 小于 30 的记录
db.users.find(
{
$or: [ { status: "A" }, { age: { $lt: 30 } } ]
}
)


2.3.3 and 和 or 联合条件查询

该语法也比较容易理解,就是将以上两点合并到一起,e.g:

db.users.find(
{
status: "A",
$or: [ { age: { $lt: 30 } }, { type: 1 } ]
}
)


3. update 更新语法

3.1 update的方法

方法说明
db.collection.updateOne()最多更新一条记录,即使filter匹配了多条记录
db.collection.updateMany()更新所有的filter匹配的记录
db.collection.replaceOne()最多替换一条记录,即使filter匹配了多条记录
db.collection.update()更新或替换一条filter的匹配记录,如果需要更新多条记录需要使用参数
multi:true
db.collections.upmethod(<filter>, <udpator>)


<filter>
语法同查询的语法。

<udpator>
语法如下:

{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}


3.2 语法实例

3.2.1 更新Document

updateOne,updateMany,update类似,这里列举一个实例:

db.users.updateOne(
{ "name": "longlong" },
{
$set: {"goodman":1}
}
)


注意:如果是
db.collenctions.update
方法,必须加
$set
操作符才能更新部分字段,否则会替换整个的document

3.2.1 替换Document

替换document文档的内容,除了
_id
字段的值,将新的document作为第二个参数传入到
db.collections.replace
db.collections.update
。替换document必须只能由
<field> : <value>
组成。

替换document可以和原document有不同的字段。新document可以忽略
_id
字段,如果新document包含该字段,那么值必须与原document保持一致。

语法实例:

db.users.replaceOne(
{ name: "abc" },
{ name: "amy", age: 34, type: 2, status: "P", favorites: { "artist": "Dali", food: "donuts" } }
)

db.users.update(
{ name: "xyz" },
{ name: "mee", age: 25, type: 1, status: "A", favorites: { "artist": "Matisse", food: "mango" } }
)<
b12a
/code>


4 删除

4.1 方法总结

方法说明
db.collection.remove()删除一条记录或者所有filter的匹配数据
db.collection.deleteOne()最多删除一条记录,即使filter匹配了多条记录
db.collection.deleteMany()删除所有的filter匹配的记录
<filter>[/code]语法同查询语法

4.2 删除的行为

删除操作不会删除索引,即使整个document删除。

原子性的操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息