找到 1349 篇文章 关于 MongoDB
509 次浏览
JavaScript 函数可以使用名为 system.js 的系统集合保存以供重用。要存储函数,请使用 db.collection.save(),让我们首先创建一个函数。以下是查询 -> db.system.js.save({ ... _id: "displayMessage", ... value: function (data) { ... return 'The Name is: ' + data; ... } ... })这将产生以下输出 -WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "displayMessage" })以下是执行存储函数的查询 -> db.eval("displayMessage('John')") 警告:db.eval 已弃用这将产生以下输出 -名称为:John
558 次浏览
要查找键未知的情况,请使用 $addField 和 $objectToArray。让我们首先创建一个包含文档的集合 -> db.demo375.insertOne( ... { ... "details":{ ... "Name":"John", ... "Age":23 ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5a0ae42ae06a1609a00b06") } > db.demo375.insertOne( ... { ... "details":{ ... "Name":"David", ... "Age":21 ... } ... } ... ); { "acknowledged" : true, "insertedId" ... 阅读更多
401 次浏览
要按另一个字段进行分组,请使用 $group 以及 $project。让我们首先创建一个包含文档的集合 -> db.demo374.insertOne( ... { ... ... "Name" : "Chris", ... "HobbyDetails" : [ ... "Reading Book", ... "Playing Football" ... ], ... "CountryName" : "US" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5a04402ae06a1609a00b04") } > db.demo374.insertOne( ... { ... ... "Name" : "Chris", ... "HobbyDetails" : [ ... ... 阅读更多
339 次浏览
让我们首先创建一个包含文档的集合 -> db.demo373.createIndex({"Name":1, "CountryName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo373.insertOne({"Name":"Chris", "Age":22, "CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e59ffde2ae06a1609a00aff") } > db.demo373.insertOne({"Name":"David", "Age":21, "CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e59ffe82ae06a1609a00b00") } > db.demo373.insertOne({"Name":"Bob", "Age":23, "CountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5e59fff42ae06a1609a00b01") } > db.demo373.insertOne({"Name":"John", "Age":21, "CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e59ffff2ae06a1609a00b02") } > db.demo373.insertOne({"Name":"Carol", "Age":23, "CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5a00082ae06a1609a00b03") }显示所有 ... 阅读更多
2K+ 次浏览
$ifNull 评估表达式,如果表达式评估为非空值,则返回表达式的值。让我们首先创建一个包含文档的集合 -> db.demo372.insertOne({"FirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e591aea2ae06a1609a00af6") } > db.demo372.insertOne({"FirstName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5e591aef2ae06a1609a00af7") } > db.demo372.insertOne({"FirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e591af42ae06a1609a00af8") } > db.demo372.insertOne({"FirstName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5e591afb2ae06a1609a00af9") }使用 find() 方法显示集合中的所有文档 -> db.demo372.find();这将产生以下输出 -{ "_id" : ObjectId("5e591aea2ae06a1609a00af6"), "FirstName" : "Chris" ... 阅读更多
1K+ 次浏览
要更改主键,您需要先删除它。使用 forEach() 以及 delete 删除,然后获取新的主键。让我们创建一个包含文档的集合 -> db.demo41.insertOne({"StudentName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5e25ce4acfb11e5c34d898e3") }使用 find() 方法显示集合中的所有文档 -> db.demo41.find();这将产生以下输出 -{ "_id" : ObjectId("5e25ce4acfb11e5c34d898e3"), "StudentName" : "Carol" }以下是更改 MongoDB 集合主键的查询 -> var next = db.demo41.find() > > next.forEach(function(s) { ... var prevId=s._id; ... delete s._id; ... ... 阅读更多
2K+ 次浏览
为此,请在 MongoDB 中使用 save()。以下是语法 -var anyVaribaleName=yourValue db.anyCollectionName.save(yourVariableName); yourVariableName;让我们首先为我们的示例创建一个对象 -> var studentDetails={"StudentName":"Chris","ListOfMarks":[56,78,89],"ListOfSubject":["MySQL","Java","MongoDB"]};让我们保存上面创建的对象“studentDetails” -> db.demo40.save(studentDetails); WriteResult({ "nInserted" : 1 })让我们显示该值 -> studentDetails;这将产生以下输出 -{ "StudentName" : "Chris", "ListOfMarks" : [ 56, 78, 89 ], "ListOfSubject" : [ "MySQL", "Java", "MongoDB" ], "_id" : ObjectId("5e177757cfb11e5c34d898e2") }
273 次浏览
让我们首先创建一个包含文档的集合 -> db.demo371.insertOne({"Name":"David", "CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57f6982ae06a1609a00af2") } > db.demo371.insertOne({"Name":"John", "CountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57f69e2ae06a1609a00af3") } > db.demo371.insertOne({"Name":"Bob", "CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57f6a42ae06a1609a00af4") } > db.demo371.insertOne({"Name":"Mike", "CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57f6ba2ae06a1609a00af5") }使用 find() 方法显示集合中的所有文档 -> db.demo371.find();这将产生以下输出 -{ "_id" : ObjectId("5e57f6982ae06a1609a00af2"), "Name" : "David", "CountryName" : "US" } { "_id" : ObjectId("5e57f69e2ae06a1609a00af3"), "Name" : "John", "CountryName" : "UK" } ... 阅读更多
123 次浏览
首先,使用 USE 命令切换到 MongoDB 中的特定数据库,如下面的语法所示 -use yourDatabaseName; db.getCollectionNames();让我们实现上述语法以显示数据库 WEB 的集合 -> use web; 切换到 db web > db.getCollectionNames();这将产生以下输出 -[ "2015-myCollection", "2015-yourCollection", "2019-employeeCollection", "addColumnDemo", "applyConditionDemo", "calculateAverage", "calculateSumOfDocument", "changeSimpleFieldDemo", "check", "checkFieldDemo", "collationExample", "compoundIndexDemo", "countandsumdemo", "creatingAliasDemo", "decreasetimeusingindex", "demo1", "demo10", "demo11", "demo12", "demo13", "demo14", "demo15", "demo16", "demo17", "demo18", ... 阅读更多