mongdb客户端创建、查询、更新、删除、实例

1、创建

db.getCollection('imovie_log').insert({
    "_id" : ObjectId("5aed61acab88cd7b1476c169"),
    "id" : NumberLong(442351752709144576),
    "account" : "admin",
    "userName" : "管理员",
    "department" : "**影业集团",
    "createDate" : ISODate("2018-05-05T15:47:56.344+08:00"),
    "module" : "权限系统,权限管理,菜单管理",
    "operationType" : "新增",
    "operationLog" : "新增菜单功能",
    "ip" : "192.168.100.170",
    "companyId" : NumberLong(1),
    "parameter" : "menuId = 441564737885437959\noperationCode = list\noperationName = 列表\noperationUrl = list\n"
})

批量创建

db.test_collection.insert( [
{"name":"abc","age":"25","status":"zxc"},
{"name":"dec","age":"19","status":"qwe"},
{"name":"asd","age":"30","status":"nmn"},
] )

2、查询

(1)and查询

db.getCollection('imovie_log').find({_id:ObjectId("5af26042b0ab7d7b8c6bb293"),account:"admin",userName:"管理员",createDate:{$gte:ISODate("2018-05-08T15:47:56.344Z"),$lte:ISODate("2018-05-09T15:47:56.344Z")}})

(2)or查询

db.getCollection('imovie_log').find({$or:[{"account":"admin"},{"userName": "管理员"}]})

(3)and和or查询

db.getCollection('imovie_log').find({"companyId": {$gt:1}, $or: [{"account":"admin"},{"userName": "管理员"}]})

3、更新

db.getCollection('imovie_log').update({id:442351752709144576},{$set:{department:"淘票票集团"}})

以上语句只会修改第一条发现的文档,如果你要修改多条相同的文档,则需要设置 multi 参数为 true。

db.getCollection('imovie_log').update({id:442351752709144576},{$set:{department:"淘票票集团"}},{multi:true})

更新单个文档

> db.test_collection.updateOne({"name":"abc"},{$set:{"age":"28"}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.test_collection.find()
{ "_id" : ObjectId("59c8ba673b92ae498a5716af"), "name" : "abc", "age" : "28", "status" : "zxc" }
{ "_id" : ObjectId("59c8ba673b92ae498a5716b0"), "name" : "dec", "age" : "19", "status" : "qwe" }
{ "_id" : ObjectId("59c8ba673b92ae498a5716b1"), "name" : "asd", "age" : "30", "status" : "nmn" }
>

更新多个文档

> db.test_collection.updateMany({"age":{$gt:"10"}},{$set:{"status":"xyz"}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.test_collection.find()
{ "_id" : ObjectId("59c8ba673b92ae498a5716af"), "name" : "abc", "age" : "28", "status" : "xyz" }
{ "_id" : ObjectId("59c8ba673b92ae498a5716b0"), "name" : "dec", "age" : "19", "status" : "xyz" }
{ "_id" : ObjectId("59c8ba673b92ae498a5716b1"), "name" : "asd", "age" : "30", "status" : "xyz" }
>

4、删除

db.getCollection('imovie_log').remove({department:"淘票票集团"})

5、创建索引

db.getCollection('filmScheduleTotal').createIndex({"terminalId":1,"stopSaleTime":1})

db.getCollection('filmScheduleTotal').ensureIndex({"terminalId":-1,"stopSaleTime":-1})

6、查看索引

db.getCollection('filmScheduleTotal').getIndexes()

7、删除索引

basCityId_-1为索引名称

db.getCollection('filmScheduleTotal').dropIndex("basCityId_-1")

猜你喜欢

转载自blog.csdn.net/qq_42093488/article/details/81710702