找到 1349 篇文章 关于 MongoDB
62 次浏览
为此,请使用 ensureIndex()。让我们创建一个包含文档的集合 -> db.demo678.ensureIndex({id:1,"details.userId":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo678.insertOne( ... { ... id:101, ... ... "details" : [ ... { ... "userId" : "1001", ... "userName":"Chris" ... }, ... { ... "userId" : "1002", ... "userName":"David" ... } ... ], ... "otherDetails" : [ ... { ... CountryName:"US", ... EmailId:["[email protected]","[email protected]"] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea4276904263e90dac943fc") }使用 find() 方法显示集合中的所有文档 -> db.demo678.find();这将产生以下输出 -{ "_id" : ObjectId("5ea4276904263e90dac943fc"), "id" : 101, "details" : [ { "userId" : "1001", "userName" : "Chris" }, { "userId" : "1002", "userName" : "David" } ], "otherDetails" : [ { "CountryName" : "US", "EmailId" : [ "[email protected]", "[email protected]" ] } ] }
234 次浏览
让我们创建一个包含文档的集合 -> db.demo677.insertOne({Value:10}); { "acknowledged" : true, "insertedId" : ObjectId("5ea421f404263e90dac943f8") } > db.demo677.insertOne({Value:50}); { "acknowledged" : true, "insertedId" : ObjectId("5ea421f704263e90dac943f9") } > db.demo677.insertOne({Value:20}); { "acknowledged" : true, "insertedId" : ObjectId("5ea421fa04263e90dac943fa") } > db.demo677.insertOne({Value:20}); { "acknowledged" : true, "insertedId" : ObjectId("5ea421fe04263e90dac943fb") }使用 find() 方法显示集合中的所有文档 -> db.demo677.find();这将产生以下输出 -{ "_id" : ObjectId("5ea421f404263e90dac943f8"), "Value" : 10 } { "_id" : ObjectId("5ea421f704263e90dac943f9"), "Value" : 50 } { "_id" : ObjectId("5ea421fa04263e90dac943fa"), "Value" : 20 } { "_id" : ... 阅读更多
168 次浏览
对于分组查询,使用 MongoDB $group 并使用 $sum 获取计数。让我们创建一个包含文档的集合 -> db.demo676.insertOne({"Marks":87}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41eed04263e90dac943f2") } > db.demo676.insertOne({"Marks":75}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef304263e90dac943f3") } > db.demo676.insertOne({"Marks":87}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef404263e90dac943f4") } > db.demo676.insertOne({"Marks":65}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef704263e90dac943f5") } > db.demo676.insertOne({"Marks":65}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef804263e90dac943f6") }使用 find() 方法显示集合中的所有文档 -> db.demo676.find();这将产生以下输出 -{ "_id" : ... 阅读更多
119 次浏览
让我们创建一个包含文档的集合 -> db.demo675.insertOne({ ... "ListOfNames":["John", "Chris", "David"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f5b404263e90dac943ef") } > db.demo675.insertOne({ "ListOfNames":["Bob", "Johnson", "Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f5c804263e90dac943f0") } > db.demo675.insertOne({ "ListOfNames":["Jace", "Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f5d304263e90dac943f1") }使用 find() 方法显示集合中的所有文档 -> db.demo675.find();这将产生以下输出 -{ "_id" : ObjectId("5ea3f5b404263e90dac943ef"), "ListOfNames" : [ "John", "Chris", "David" ] } { "_id" : ObjectId("5ea3f5c804263e90dac943f0"), "ListOfNames" : [ "Bob", "Johnson", "Sam" ] } { "_id" : ObjectId("5ea3f5d304263e90dac943f1"), "ListOfNames" : ... 阅读更多
128 次浏览
$or 运算符对两个或多个表达式的数组执行逻辑 OR 操作。让我们创建一个包含文档的集合 -> db.demo674.insertOne({Name:"Chris", Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f33604263e90dac943eb") } > db.demo674.insertOne({Name:"David", Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f33c04263e90dac943ec") } > db.demo674.insertOne({Name:"Bob", Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f34204263e90dac943ed") } > db.demo674.insertOne({Name:"John", Age:24}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f34804263e90dac943ee") }使用 find() 方法显示集合中的所有文档 -> db.demo674.find();这将产生以下输出 -{ "_id" : ObjectId("5ea3f33604263e90dac943eb"), "Name" : "Chris", ... 阅读更多
66 次浏览
使用 max 字段来限制集合中的文档数量。以下是如何在限定集合中使用 max 字段的查询 -> db.createCollection("demo673", { capped : true, size : 100, max :50 } ) { "ok" : 1 }让我们创建一个包含文档的集合 -> db.demo673.insertOne({Name:"John", Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ec7304263e90dac943e8") } > db.demo673.insertOne({Name:"Bob", Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ec7804263e90dac943e9") } > db.demo673.insertOne({Name:"David", Age:20}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ec7f04263e90dac943ea") }使用 find() 方法显示集合中的所有文档 ... 阅读更多
195 次浏览
要获取特定文档,请在 MongoDB find() 中使用点表示法。让我们创建一个包含文档的集合 -> db.demo672.insertOne({Brand:[{CategoryName:"Mobile", "Name":"Oppo"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3ea9b04263e90dac943e5") } > db.demo672.insertOne({Brand:[{CategoryName:"Mobile", "Name":"Samsung"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3eaa404263e90dac943e6") } > db.demo672.insertOne({Brand:[{CategoryName:"Mobile", "Name":"OnePlus"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3eacc04263e90dac943e7") }使用 find() 方法显示集合中的所有文档 -> db.demo672.find();这将产生以下输出 -{ "_id" : ObjectId("5ea3ea9b04263e90dac943e5"), "Brand" : [ { "CategoryName" : "Mobile", "Name" : "Oppo" } ] } { "_id" : ObjectId("5ea3eaa404263e90dac943e6"), "Brand" : [ { ... 阅读更多
752 次浏览
要访问 MongoDB 中的子文档,请使用带点表示法的 find()。让我们创建一个包含文档的集合 -> db.demo670.insertOne({ ... id:101, ... "details": ... { ... Name:"Chris", ... Age:21, ... CountryName:"US", ... SubjectName:"MongoDB" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea3e31d04263e90dac943de") } > db.demo670.insertOne({ id:102, "details": { Name:"David", Age:22, CountryName:"UK", SubjectName:"MySQL" } } ); { "acknowledged" : true, "insertedId" : ObjectId("5ea3e33604263e90dac943df") }使用 find() 方法显示集合中的所有文档 -> db.demo670.find();这将产生以下输出 -{ "_id" : ObjectId("5ea3e31d04263e90dac943de"), "id" : 101, "details" : { "Name" ... 阅读更多
108 次浏览
要从集合中获取最大元素,请按降序排序并使用限制。让我们创建一个包含文档的集合 -> > db.demo669.insertOne({"Marks":76}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3133c04263e90dac943d9") } > db.demo669.insertOne({"Marks":55}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3133f04263e90dac943da") } > db.demo669.insertOne({"Marks":89}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3134204263e90dac943db") } > db.demo669.insertOne({"Marks":88}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3134504263e90dac943dc") } > db.demo669.insertOne({"Marks":79}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3134e04263e90dac943dd") }使用 find() 方法显示集合中的所有文档 -> db.demo669.find();这将产生以下输出 ... 阅读更多