找到 1349 篇文章 适用于 MongoDB
355 次查看
为此,请在 MongoDB 中使用 aggregate()。让我们创建一个包含文档的集合 -> db.demo718.insertOne( ... { ... "id":101, ... "details": ... { ... "OtherDetails": ["Chris", "Mike", "Sam"], "GroupName": ["Group-1"], "Info": [] ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eaae25843417811278f5880") } > db.demo718.insertOne( ... { ... "id":102, ... "details": ... { ... "OtherDetails": ["Chris", "David"], "GroupName": ["Group-1"], "Info": [] ... ... 阅读更多
226 次查看
对于聚合,请在 MongoDB 中使用 aggregate()。使用 $group 对日期进行分组。让我们创建一个包含文档的集合 -> db.demo717.insertOne( ... { ... "shippingdetails": ... [ ... { ... duedate:"2020-04-29 22:33:04", ... }, ... { ... duedate:"2020-03-29 22:33:04", ... }, ... { ... duedate:"2020-04-29 22:33:04", ... }, ... ... 阅读更多
522 次查看
要删除子集合中的对象,请在 MongoDB 中使用 $pull。让我们创建一个包含文档的集合 -> db.demo715.insertOne({ ... _id:1, ... details : ... [ ... { 'id' : '101', ... 'Information' : [ ... { ... 'studentid' : '102', ... "Name":"Bob" ... }, ... { ... 'studentid' : '103', ... "Name":"Chris" ... 阅读更多
99 次查看
让我们创建一个包含文档的集合 -> db.demo714.insertOne({FirstName:"Chris", LastName:"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea9a2da85324c2c98cc4c2b") } > db.demo714.insertOne({FirstName:"Adam", LastName:"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea9a2e585324c2c98cc4c2c") } > db.demo714.insertOne({FirstName:"David", LastName:"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea9a2ed85324c2c98cc4c2d") } > db.demo714.insertOne({FirstName:"John", LastName:"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea9a2f785324c2c98cc4c2e") }显示使用 find() 方法从集合中获取所有文档 -> db.demo714.find();这将产生以下输出 -{ "_id" : ObjectId("5ea9a2da85324c2c98cc4c2b"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5ea9a2e585324c2c98cc4c2c"), "FirstName" : "Adam", "LastName" : "Smith" } { ... 阅读更多
450 次查看
要添加新字段,请在 MongoDB 中使用 $addFields。让我们创建一个包含文档的集合 -> db.demo712.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea85f675d33e20ed1097b82") } > db.demo712.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea85f6a5d33e20ed1097b83") } > db.demo712.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea85f6e5d33e20ed1097b84") }显示使用 find() 方法从集合中获取所有文档 -> db.demo712.find();这将产生以下输出 -{ "_id" : ObjectId("5ea85f675d33e20ed1097b82"), "Name" : "John" } { "_id" : ObjectId("5ea85f6a5d33e20ed1097b83"), "Name" : "Chris" } { "_id" : ObjectId("5ea85f6e5d33e20ed1097b84"), "Name" : "Bob" }以下是在... 阅读更多
2K+ 次查看
让我们创建一个包含文档的集合 -> db.demo711.insertOne({Name:"John", "Marks":75, Age:21, status:"Active"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea85c215d33e20ed1097b7e") } > db.demo711.insertOne({Name:"Chris", "Marks":55, Age:22, status:"Active"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea85c2c5d33e20ed1097b7f") } > db.demo711.insertOne({Name:"Bob", "Marks":45, Age:20, status:"Inactive"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea85c3e5d33e20ed1097b80") } > db.demo711.insertOne({Name:"David", "Marks":85, Age:23, status:"Active"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea85c775d33e20ed1097b81") }显示使用 find() 方法从集合中获取所有文档 -> db.demo711.find();这将产生以下输出 -{ "_id" : ObjectId("5ea85c215d33e20ed1097b7e"), "Name" : "John", "Marks" : 75, "Age" : 21, "status" : ... 阅读更多
948 次查看
$natural 返回自然顺序的文档。要反转项目,请使用 $natural:-1。让我们创建一个包含文档的集合 -> db.demo710.insertOne({id:101, Name:"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea83a855d33e20ed1097b7a") } > db.demo710.insertOne({id:102, Name:"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea83a8d5d33e20ed1097b7b") } > db.demo710.insertOne({id:103, Name:"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea83a935d33e20ed1097b7c") } > db.demo710.insertOne({id:104, Name:"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea83a9b5d33e20ed1097b7d") }显示使用 find() 方法从集合中获取所有文档 -> db.demo710.find();这将产生以下输出 -{ "_id" : ObjectId("5ea83a855d33e20ed1097b7a"), "id" : 101, "Name" : ... 阅读更多
118 次查看
要获取排序后的成绩列表,请使用 $sort。使用 $limit: 2 仅显示成绩最低的两条文档。让我们创建一个包含文档的集合 -> db.demo709.insertOne({Name:"John", "Marks":75}); { "acknowledged" : true, "insertedId" : ObjectId("5ea839005d33e20ed1097b76") } > db.demo709.insertOne({Name:"Chris", "Marks":45}); { "acknowledged" : true, "insertedId" : ObjectId("5ea839075d33e20ed1097b77") } > db.demo709.insertOne({Name:"David", "Marks":54}); { "acknowledged" : true, "insertedId" : ObjectId("5ea839125d33e20ed1097b78") } > db.demo709.insertOne({Name:"Bob", "Marks":69}); { "acknowledged" : true, "insertedId" : ObjectId("5ea839295d33e20ed1097b79") }使用 find() 方法显示集合中的所有文档 -> db.demo709.find();这将产生以下输出 -{ "_id" ... 阅读更多