找到关于数据库的6705篇文章
327 次浏览
ObjectId 不接受种子字符串。您需要使用 _id - StringValue。让我们创建一个包含文档的集合 −> db.demo667.insertOne({_id:"Chris"}); { "acknowledged" : true, "insertedId" : "Chris" } > db.demo667.insertOne({_id:"David"}); { "acknowledged" : true, "insertedId" : "David" } > db.demo667.insertOne({_id:"Chris"}); 2020-04-23T22:01:23.268+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo667 index: _id_ dup key: { : "Chris" } : WriteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: test.demo667 index: _id_ dup key: { : \"Chris\" }", "op" : { "_id" : ... 阅读更多
2K+ 次浏览
要仅显示具有特定薪水的员工姓名,请在 MongoDB 中设置 $in 薪水并获取姓名。让我们创建一个包含文档的集合> db.demo666.insertOne({"EmployeeName":"John", "EmployeeSalary":25000}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1c04824113ea5458c7d0d") } > db.demo666.insertOne({"EmployeeName":"Chris", "EmployeeSalary":35000}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1c05524113ea5458c7d0e") } > db.demo666.insertOne({"EmployeeName":"David", "EmployeeSalary":65000}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1c06024113ea5458c7d0f") } > db.demo666.insertOne({"EmployeeName":"Carol", "EmployeeSalary":40000}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1c06f24113ea5458c7d10") }使用 find() 方法显示集合中的所有文档 −> db.demo666.find();这将产生以下输出 −{ "_id" : ObjectId("5ea1c04824113ea5458c7d0d"), "EmployeeName" ... 阅读更多
96 次浏览
要搜索范围,请使用带有 limit() 的 sort()。让我们创建一个包含文档的集合 −> db.demo665.insertOne({"Value":10}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1bf1424113ea5458c7d08") } > db.demo665.insertOne({"Value":15}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1bf1b24113ea5458c7d09") } > db.demo665.insertOne({"Value":55}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1bf1e24113ea5458c7d0a") } > db.demo665.insertOne({"Value":25}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1bf2324113ea5458c7d0b") } > db.demo665.insertOne({"Value":20}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1bf2b24113ea5458c7d0c") }使用 find() 方法显示集合中的所有文档 −> db.demo665.find();这将产生以下输出 −{ "_id" : ObjectId("5ea1bf1424113ea5458c7d08"), "Value" : 10 ... 阅读更多
398 次浏览
它保存在特殊的 system.js 集合中。为此,请使用 db.system.js.save()。以下是语法 −db.system.js.save({ _id: "anyFunctionName", value: function (returnValue) { return ‘yourMessage ' + returnValue; } })让我们实现上述语法。以下是查询 −> db.system.js.save({ ... _id: "returnValue", ... value: function (data) { ... return 'The value==== ' + data; ... } ... }) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })以下是调用上述函数以打印实际数据的查询 −> db.eval("returnValue(20)") WARNING: db.eval is deprecated这将产生以下输出 −The value==== 20
734 次浏览
要计算文档数量,请在 MongoDB 中使用 count()。让我们创建一个包含文档的集合 −> db.demo664.insertOne({_id:1, ClientName:"Chris"}); { "acknowledged" : true, "insertedId" : 1 } > db.demo664.insertOne({_id:2, ClientName:"Bob"}); { "acknowledged" : true, "insertedId" : 2 } > db.demo664.insertOne({_id:3, ClientName:"Sam"}); { "acknowledged" : true, "insertedId" : 3 } > db.demo664.insertOne({_id:4, ClientName:"David"}); { "acknowledged" : true, "insertedId" : 4 }使用 find() 方法显示集合中的所有文档 −> db.demo664.find();这将产生以下输出 −{ "_id" : 1, "ClientName" : "Chris" } { "_id" : 2, "ClientName" : "Bob" } { "_id" : 3, "ClientName" : ... 阅读更多
377 次浏览
对于 MongoDB 中的批量插入,请使用 initializeUnorderedBulkOp()。让我们创建一个包含文档的集合 −> var bulkInsertDoc = db.demo663.initializeUnorderedBulkOp(); > bulkInsertDoc.insert( { Name: "John", CountryName:"US"} ); > bulkInsertDoc.insert( { Name: "Chris", CountryName:"UK"} ); > bulkInsertDoc.insert( { Name: "David", CountryName:"AUS"} ); > bulkInsertDoc.execute(); BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })使用 find() 方法显示集合中的所有文档 −> db.demo663.find();这将产生以下输出 −{ "_id" : ... 阅读更多
230 次浏览
要过滤掉子文档,请使用 MongoDB 聚合,并在其中使用 $unwind。让我们创建一个包含文档的集合 −> db.demo662.insertOne( ... { ... "details":[ ... { ... Name:"Chris", ... Marks:35 ... }, ... { ... Name:"Bob", ... Marks:45 ... }, ... { ... Name:"David", ... Marks:30 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea1b2be24113ea5458c7d04") }使用 ... 阅读更多
388 次浏览
为了更快地搜索,请创建索引。为此,请使用 createIndex()。让我们创建一个包含文档的集合 −> db.demo661.createIndex({ListOfName:1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo661.insertOne({_id:1, ListOfName:["John", "Robert", "David"]}); { "acknowledged" : true, "insertedId" : 1 } > db.demo661.insertOne({_id:2, ListOfName:["Mike", "Sam"]}); { "acknowledged" : true, "insertedId" : 2 } > db.demo661.insertOne({_id:3, ListOfName:["John", "David", "Bob"]}); { "acknowledged" : true, "insertedId" : 3 }使用 find() 方法显示集合中的所有文档 −> db.demo661.find();这将产生以下输出 −{ "_id" : 1, "ListOfName" : [ "John", ... 阅读更多
350 次浏览
您可以使用 $group。让我们创建一个包含文档的集合 −> db.demo659.insertOne({Name:"Chris", CountryName:"US", "Marks":50}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1a50724113ea5458c7cf9") } > db.demo659.insertOne({Name:"David", CountryName:"US", "Marks":60}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1a50724113ea5458c7cfa") } > db.demo659.insertOne({Name:"Mike", CountryName:"US", "Marks":55}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1a50724113ea5458c7cfb") } > db.demo659.insertOne({Name:"Chris", CountryName:"UK", "Marks":75}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1a50724113ea5458c7cfc") } > db.demo659.insertOne({Name:"David", CountryName:"UK", "Marks":54}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1a50724113ea5458c7cfd") } > db.demo659.insertOne({Name:"Mike", CountryName:"UK", "Marks":72}); { "acknowledged" : true, "insertedId" : ObjectId("5ea1a50824113ea5458c7cfe") }显示集合中的所有文档 ... 阅读更多
139 次浏览
对于唯一索引,在创建索引时设置 unique 为 true。让我们创建一个包含文档的集合:-> db.demo658.createIndex({FirstName:1}, {unique:true, sparse:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > > db.demo658.insertOne({"FirstName":"John", "LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea067144deddd72997713d9") } > db.demo658.insertOne({"FirstName":"Adam", "LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea0671c4deddd72997713da") } > db.demo658.insertOne({"FirstName":"John", "LastName":"Doe"}); 2020-04-22T21:17:46.072+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: onlinecustomertracker.demo658 index: FirstName_1 dup key: { : "John" } : WriteError({ "index" : 0, "code" : 11000, ... 阅读更多