找到 1349 篇文章 相关 MongoDB
180 次浏览
使用 $pull 从 MongoDB 文档中删除数组元素,如下面的语法所示:db.yourCollectionName.update( { }, { $pull: { yourFieldName: yourValue }}, {multi:true });让我们先创建一个包含文档的集合:>db.removeArrayElementsDemo.insertOne({"AllPlayerName":["John", "Sam", "Carol", "David"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd90d011a844af18acdffc1") } >db.removeArrayElementsDemo.insertOne({"AllPlayerName":["Chris", "Robert", "John", "Mike"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd90d2e1a844af18acdffc2") }以下是如何使用 find() 方法显示集合中所有文档的查询:> db.removeArrayElementsDemo.find().pretty();这将产生以下输出:{ "_id" : ObjectId("5cd90d011a844af18acdffc1"), "AllPlayerName" : [ "John", ... 阅读更多
178 次浏览
您可以为此使用 $exists 运算符。让我们先创建一个包含文档的集合:>db.checkFieldExistsDemo.insertOne({"StudentFirstName":"John", "StudentGender":"Male", "StudentMongoDBScore":89}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909611a844af18acdffbd") } >db.checkFieldExistsDemo.insertOne({"StudentFirstName":"Emma", "StudentGender":"Female", "StudentMongoDBScore":58}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909781a844af18acdffbe") } >db.checkFieldExistsDemo.insertOne({"StudentFirstName":"Carol", "StudentGender":"Male", "StudentMongoDBScore":77}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909871a844af18acdffbf") } >db.checkFieldExistsDemo.insertOne({"StudentFirstName":"David", "StudentMongoDBScore":98}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909a31a844af18acdffc0") }以下是如何使用 find() 方法显示集合中所有文档的查询:> db.checkFieldExistsDemo.find().pretty();这将产生以下输出:{ "_id" : ObjectId("5cd909611a844af18acdffbd"), "StudentFirstName" : "John", "StudentGender" ... 阅读更多
109 次浏览
是的,我们可以使用以下 MongoDB 语法避免 _id:db.yourCollectionName.find({}, { _id:0});让我们先创建一个包含文档的集合:>> db.excludeIdDemo.insertOne({"CustomerName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f62c1a844af18acdffb9") } > db.excludeIdDemo.insertOne({"CustomerName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f6311a844af18acdffba") } > db.excludeIdDemo.insertOne({"CustomerName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f6351a844af18acdffbb") } > db.excludeIdDemo.insertOne({"CustomerName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f6381a844af18acdffbc") }以下是如何使用 find() 方法显示集合中所有文档的查询:> db.excludeIdDemo.find().pretty();这将产生以下输出:{ "_id" : ObjectId("5cd7f62c1a844af18acdffb9"), "CustomerName" : "Larry" } ... 阅读更多
1K+ 次浏览
使用 $set 替换数组中的值。让我们先创建一个包含文档的集合:> db.replaceValueInArrayDemo.insertOne({"StudentScores":[45, 56, 78]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f0421a844af18acdffb7") } > db.replaceValueInArrayDemo.insertOne({"StudentScores":[33, 90, 67]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f0521a844af18acdffb8") }以下是如何使用 find() 方法显示集合中所有文档的查询:> db.replaceValueInArrayDemo.find().pretty();这将产生以下输出:{ "_id" : ObjectId("5cd7f0421a844af18acdffb7"), "StudentScores" : [ 45, 56, 78 ] } { "_id" : ObjectId("5cd7f0521a844af18acdffb8"), "StudentScores" : [ ... 阅读更多
855 次浏览
让我们先创建一个包含文档的集合:> db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"JOHN Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7edef1a844af18acdffb2") } > db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"SAM Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ee011a844af18acdffb3") } > db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"CAROL Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ee101a844af18acdffb4") } > db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"Bob Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ee351a844af18acdffb5") } > db.upperCaseFiveLetterDemo.insertOne({"StudentFullName":"DAVID Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ee451a844af18acdffb6") }以下是如何使用 find() 方法显示集合中所有文档的查询:> db.upperCaseFiveLetterDemo.find().pretty();这将产生以下输出:{ "_id" : ... 阅读更多
83 次浏览
$gt 用于大于,选择字段值大于指定值的那些文档。让我们先创建一个包含文档的集合:> db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":1000, "PlayerLevel":2}, "PlayerName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7eba41a844af18acdffa9") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":0, "PlayerLevel":1}, "PlayerName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebbb1a844af18acdffaa") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":-10, "PlayerLevel":0}, "PlayerName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebd41a844af18acdffab") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":1, "PlayerLevel":1}, "PlayerName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebe31a844af18acdffac") }以下是如何使用 ... 阅读更多
521 次浏览
我们将在这里使用 Null 类型。以下是带别名的空值类型:类型数字别名双精度1“double”字符串2“string”对象3“object”数组4“array”二进制数据5“binData”未定义6“undefined”ObjectId7“objectId”布尔8“bool”日期9“date”空10“null”正则表达式11“regex”以下是类型 10(即 null)的语法:db.yourCollectionName.find({"yourFieldName":{ $type: 10 } });以上语法将仅查找具有空值的那些文档。让我们先创建一个包含文档的集合:> db.mongoDbEqualDemo.insertOne({"Age":34}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7e9121a844af18acdffa3") } > db.mongoDbEqualDemo.insertOne({"Age":""}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7e9161a844af18acdffa4") } > db.mongoDbEqualDemo.insertOne({"Age":null}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7e9191a844af18acdffa5") } > db.mongoDbEqualDemo.insertOne({"Age":56}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7e91e1a844af18acdffa6") } > db.mongoDbEqualDemo.insertOne({}); ... 阅读更多
360 次浏览
首先让我们看看删除集合的语法 - db.getCollection("yourCollectionNameWithTwoDashes").drop();为了演示,我们将创建一个名称中包含两个连字符的集合,如下所示 -> db.createCollection("company--EmployeeInformation"); { "ok" : 1 }创建上述集合“company--EmployeeInformation”,并添加文档。以下为查询:>db.getCollection("company--EmployeeInformation").insertOne({"CompanyName":"Amazon", "EmployeeName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c5ff6d78f205348bc654") } >db.getCollection("company--EmployeeInformation").insertOne({"CompanyName":"Google", "EmployeeName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c60b6d78f205348bc655") }以下为使用 find() 方法显示集合中所有文档的查询 -> db.getCollection("company--EmployeeInformation").find();这将产生以下输出 -{ "_id" : ObjectId("5cd7c5ff6d78f205348bc654"), "CompanyName" : "Amazon", "EmployeeName" : "Chris" } { ... 阅读更多
295 次浏览
$exists 用于检查字段是否存在,而 $ne 用于不等于条件。首先让我们创建一个包含文档的集合 -> db.existsDemo.insertOne({"Name":"Chris", "Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c3916d78f205348bc650") } > db.existsDemo.insertOne({"Name":"", "Age":null}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c39a6d78f205348bc651") } > db.existsDemo.insertOne({"Name":null, "Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c3a66d78f205348bc652") } > db.existsDemo.insertOne({"Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c3c36d78f205348bc653") }以下为使用 find() 方法显示集合中所有文档的查询 -> db.existsDemo.find().pretty();这将产生以下输出 -{ "_id" : ObjectId("5cd7c3916d78f205348bc650"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5cd7c39a6d78f205348bc651"), "Name" : "", "Age" ... 阅读更多
404 次浏览
首先让我们创建一个包含文档的集合 -> db.findOneWorkingDemo.insertOne({"ClientId":1, "ClientName":"Larry", "ClientAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c1716d78f205348bc64d") } > db.findOneWorkingDemo.insertOne({"ClientId":2, "ClientName":"Chris", "ClientAge":28}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c17d6d78f205348bc64e") } > db.findOneWorkingDemo.insertOne({"ClientId":3, "ClientName":"Robert", "ClientAge":34}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7c1896d78f205348bc64f") }以下为使用 find() 方法显示集合中所有文档的查询 -> db.findOneWorkingDemo.find().pretty();这将产生以下输出 -{ "_id" : ObjectId("5cd7c1716d78f205348bc64d"), "ClientId" : 1, "ClientName" : "Larry", "ClientAge" : 26 } { "_id" : ObjectId("5cd7c17d6d78f205348bc64e"), "ClientId" : 2, ... 阅读更多