找到 1349 篇文章 关于 MongoDB
461 次浏览
让我们创建一个包含文档的集合 -> db.demo196.insertOne( ... { ... ... "Id" : "101", ... "details" : [ ... { ... "FirstName" : "Chris", ... "LastName" : "Brown", ... "Score" : 45 ... }, ... { ... "FirstName" : "David", ... "LastName" : "Miller", ... "Score" : 87 ... ... 阅读更多
347 次浏览
要返回特定值的列表,请使用 map()。让我们创建一个包含文档的集合 -> db.demo195.insertOne({"Subject":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3af3f203d395bdc21346d4") } > db.demo195.insertOne({"Subject":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3af3f703d395bdc21346d5") } > db.demo195.insertOne({"Subject":"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3af3fa03d395bdc21346d6") }显示使用 find() 方法从集合中显示所有文档 -> db.demo195.find();这将产生以下输出 -{ "_id" : ObjectId("5e3af3f203d395bdc21346d4"), "Subject" : "MySQL" } { "_id" : ObjectId("5e3af3f703d395bdc21346d5"), "Subject" : "MongoDB" } { "_id" : ObjectId("5e3af3fa03d395bdc21346d6"), "Subject" : "Java" }以下是返回... 阅读更多
149 次浏览
让我们首先创建一个包含文档的集合 -> db.demo194.insertOne( ... { ... "_id": 101, ... "details": { ... "otherDetails": { ... "List1": ["MongoDB", "MySQL"], ... "List2": ["Java"], ... "List3": ["MongoDB", "C"] ... } ... } ... } ...); { "acknowledged" : true, "insertedId" : 101 } > db.demo194.insertOne( {"_id": 102, "details": { "otherDetails": { "List1": ["Java", "C"], "List2": ["C++"], "List3": ["Python", "Spring"] } } } ... 阅读更多
197 次浏览
要提高查询的执行时间,请使用索引以及 unique:true。让我们创建一个包含文档的集合 -> db.demo193.createIndex({"LastName":1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo193.insertOne({"FirstName":"John", "LastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ade1803d395bdc21346d1") } > db.demo193.insertOne({"FirstName":"John", "LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ade1f03d395bdc21346d2") } > db.demo193.insertOne({"FirstName":"David", "LastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ade2803d395bdc21346d3") }显示使用 find() 方法从集合中显示所有文档 -> db.demo193.find();这将产生以下输出 -{ "_id" ... 阅读更多
148 次浏览
要对多个字段进行分组,请在 MongoDB 中使用 $group。让我们创建一个包含文档的集合 -> db.demo192.insertOne({"Name":"Chris", "Age":22}); { "acknowledged" : true, "insertedId" : ObjectId("5e3adb9f03d395bdc21346cd") } > db.demo192.insertOne({"Name":"David", "Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e3adba103d395bdc21346ce") } > db.demo192.insertOne({"Name":"Chris", "Age":22}); { "acknowledged" : true, "insertedId" : ObjectId("5e3adba503d395bdc21346cf") } > db.demo192.insertOne({"Name":"Mike", "Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e3adbbf03d395bdc21346d0") }显示使用 find() 方法从集合中显示所有文档 -> db.demo192.find();这将产生以下输出 -{ "_id" : ObjectId("5e3adb9f03d395bdc21346cd"), "Name" : "Chris", "Age" : 22 } { "_id" : ObjectId("5e3adba103d395bdc21346ce"), ... 阅读更多
587 次浏览
要获取 MongoDB 中游标的值,请使用 hasNext()。让我们创建一个包含文档的集合 -> db.demo191.insertOne({"EmployeeId":1, "EmployeeName":"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad95303d395bdc21346c5") } > db.demo191.insertOne({"EmployeeId":2, "EmployeeName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad95f03d395bdc21346c6") } > db.demo191.insertOne({"EmployeeId":1, "EmployeeName":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad96803d395bdc21346c7") } > db.demo191.insertOne({"EmployeeId":1, "EmployeeName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad97003d395bdc21346c8") }显示使用 find() 方法从集合中显示所有文档 -> db.demo191.find();这将产生以下输出 -{ "_id" : ObjectId("5e3ad95303d395bdc21346c5"), "EmployeeId" : 1, "EmployeeName" : "Chris ... 阅读更多
506 次浏览
要使用多个键实现聚合,请使用 aggregate() 以及 $group。让我们创建一个包含文档的集合 -> db.demo190.insertOne( ... { ... ... "DueDate" : ISODate("2020-01-01"), ... "Value" : 10, ... "Name" : "Chris" ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad76403d395bdc21346bf") } > > db.demo190.insertOne( ... { ... ... "DueDate" : ISODate("2020-02-05"), ... "Value" : 30, ... "Name" : "David" ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad76403d395bdc21346c0") } > db.demo190.insertOne( ... { ... 阅读更多
389 次浏览
要查找多次出现的相同值,请在 MongoDB 中使用 $where。让我们创建一个包含文档的集合 -> db.demo188.insertOne( ... { ... "ListOfData":[ ... {"Data": 100}, ... {"Data": 200}, ... {"Data": 100} ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad1c203d395bdc21346bd") } > db.demo188.insertOne( ... { ... "ListOfData":[ ... {"Data": 100}, ... {"Data": 200}, ... {"Data": 300} ... } ...); { ... 阅读更多
143 次浏览
让我们首先创建一个包含文档的集合 -> db.demo187.insertOne( ... { ... "_id" : "101", ... "Details" : [ ... { "Subject" : "MongoDB" }, ... { "Subject" : "MySQL" } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "101" } > db.demo187.insertOne( ... { ... "_id" : "102", ... "Details" : [ ... { } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "102" ... 阅读更多