插入
插入一条数据
> db.fruit.insertOne({"name": "Apple"})
{
"acknowledged" : true,
"insertedId" : ObjectId("5e9abd1db647be1aca615bb4")
}
acknowledged 表示插入成功,
insertedId 是该插入数据的 _id 字段。
插入多条数据
> db.fruit.insertMany([{name: "apple"},{name: "pear"},{name: "orange"}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5e9abdb1b647be1aca615bb5"),
ObjectId("5e9abdb1b647be1aca615bb6"),
ObjectId("5e9abdb1b647be1aca615bb7")
]
}
返回同理, acknowledged 表示插入成功。
insertedIds 是插入数据的 _id 字段列表。
查询
关于 find
- find 是 mongo 中查询数据的基本命令,相当于 sql 中的 select。
- find 返回的是游标。
find 示例
> db.fruit.find()
{ "_id" : ObjectId("5e9abd1db647be1aca615bb4"), "name" : "Apple" }
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb6"), "name" : "pear" }
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb7"), "name" : "orange" }
> // 单条件查询
> db.fruit.find({"name": "apple"})
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }
> // 多条件 and 进行查询
> db.fruit.find({"name": "apple", "_id": ObjectId("5e9abdb1b647be1aca615bb5")})
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }
> // 另外一种 and 逻辑查询
> db.fruit.find({$and:[ {"name":"apple"}, {"_id": ObjectId("5e9abdb1b647be1aca615bb5")} ]})
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }
> // or 查询
> db.fruit.find({"$or":[{"name": "apple"}, {"name": "Apple"} ] } )
{ "_id" : ObjectId("5e9abd1db647be1aca615bb4"), "name" : "Apple" }
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }
> // 按照正则表达式进行查找
> db.fruit.find({"name": /^a/})
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }
> db.fruit.find({"name": /^a|A/})
{ "_id" : ObjectId("5e9abd1db647be1aca615bb4"), "name" : "Apple" }
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }
查询中的比较:
> // 大于
> db.orders.find({"userId":{"$gt": 3300}}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a545368f69de2b4d36e"), "userId" : 3573 }
> // 大于等于
> db.orders.find({"userId":{"$gte": 3300}}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a545368f69de2b4d36e"), "userId" : 3573 }
> // 小于
> db.orders.find({"userId":{"$lt": 3300}}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a545368f69de2b4d36f"), "userId" : 2500 }
> // 小于等于
> db.orders.find({"userId":{"$lte": 3300}}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a545368f69de2b4d36f"), "userId" : 2500 }
> // 等于
> db.orders.find({"userId":{"$eq": 3300}}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a5850fc769de3e19f33"), "userId" : 3300 }
> db.orders.find({"userId":3300}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a5850fc769de3e19f33"), "userId" : 3300 }
> // 不等于
> db.orders.find({"userId":{"$ne": 3300}}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a545368f69de2b4d36e"), "userId" : 3573 }
使用 find 搜索子文档
> db.fruit.insertOne({"name": "apple", "from": {"country": "China", "province": "Henan"}} )
{
"acknowledged" : true,
"insertedId" : ObjectId("5e9ac726b647be1aca615bb9")
}
> db.fruit.find({"name": {"$in": ["apple", "Apple"]}, "from.country": "China"})
{ "_id" : ObjectId("5e9ac726b647be1aca615bb9"), "name" : "apple", "from" : { "country" : "China", "province" : "Henan" } }
>
注意:第二条不会击中: