找到关于 MongoDB 的1349 篇文章
319 次浏览
您可以使用聚合框架。让我们首先创建一个包含文档的集合 -> db.getArrayDemo.insertOne( { "CustomerId":101, "CustomerDetails":[ { "CustomerName":"Larry", "CustomerFriendDetails":[ { "CustomerFriendName":"Sam" }, { "CustomerFriendName":"Robert" } ... 阅读更多
477 次浏览
您可以为此使用 $gt 运算符。让我们首先创建一个包含文档的集合 -> db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[89, 43, 32, 45]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9e9f9b50a6c6dd317adb3") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[32, 33, 34, 40]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9ea13b50a6c6dd317adb4") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[45, 56, 66, 69]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9ea25b50a6c6dd317adb5") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[46, 66, 77, 88]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9ea3cb50a6c6dd317adb6") }以下是使用 find() 方法显示集合中所有文档的查询 -> db.arrayElementsNotGreaterThanDemo.find().pretty();这将产生以下输出 -{ ... 阅读更多
85 次浏览
是的,您可以将 capped 参数与最大大小一起使用。以下是语法 -db.createCollection("yourCollectionName", {capped:true, size:yourSizeInBytes, max:howManyRecordsYouWant})让我们首先创建一个 capped:true 的集合 -> db.createCollection("limitTheNumberOfRecordsDemo", {capped:true, size:200024, max:3}) { "ok" : 1 }我们现在将创建一个包含文档的集合 -> db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"James Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9e601b50a6c6dd317adad") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"Sam Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9e60bb50a6c6dd317adae") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9e612b50a6c6dd317adaf") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"Carol Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9e61ab50a6c6dd317adb0") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"Adam Smith"}); { "acknowledged" : ... 阅读更多
98 次浏览
要在 MongoDB 中获取特定字段作为结果,您可以使用 findOne()。以下是语法 -db.yourCollectionName.findOne({"yourFieldName1":yourValue}, {yourFieldName2:1});让我们首先创建一个包含文档的集合 -> db.particularFieldDemo.insertOne({"EmployeeName":"John Smith", "EmployeeAge":26, "EmployeeTechnology":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9b4abb50a6c6dd317ada2") } > db.particularFieldDemo.insertOne({"EmployeeName":"Chris Brown", "EmployeeAge":28, "EmployeeTechnology":"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9b4d2b50a6c6dd317ada3") } > db.particularFieldDemo.insertOne({"EmployeeName":"David Miller", "EmployeeAge":30, "EmployeeTechnology":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9b4e8b50a6c6dd317ada4") } > db.particularFieldDemo.insertOne({"EmployeeName":"John Doe", "EmployeeAge":31, "EmployeeTechnology":"C++"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9b527b50a6c6dd317ada5") }以下是使用... 阅读更多
145 次浏览
让我们首先创建一个包含文档的集合,其中一个字段是 StudentName -> db.lowerCaseDemo.insertOne({"StudentName":"JOHN SMith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9a86fb50a6c6dd317ad9f") } > db.lowerCaseDemo.insertOne({"StudentName":"CAROL TAYLor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9a88fb50a6c6dd317ada0") } > db.lowerCaseDemo.insertOne({"StudentName":"DAVID Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9a89fb50a6c6dd317ada1") }以下是使用 find() 方法显示集合中所有文档的查询 -> db.lowerCaseDemo.find().pretty();这将产生以下输出 -{ "_id" : ObjectId("5cd9a86fb50a6c6dd317ad9f"), "StudentName" : "JOHN SMith" } { "_id" : ObjectId("5cd9a88fb50a6c6dd317ada0"), "StudentName" : "CAROL TAYLor" } { ... 阅读更多
726 次浏览
使用带点表示法的 find() 执行递归搜索。让我们首先创建一个包含文档的集合 -> db.findOperationDemo.insertOne({"ClientDetails":[{"ClientId":101, "ClientName":"Chris"}, {"ClientId":102, "ClientName":"Robert"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9a118b50a6c6dd317ad99") } > db.findOperationDemo.insertOne({"ClientDetails":[{"ClientId":110, "ClientName":"David"}, {"ClientId":112, "ClientName":"Mike"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9a12fb50a6c6dd317ad9a") }以下是使用 find() 方法显示集合中所有文档的查询 -> db.findOperationDemo.find().pretty();这将产生以下输出 -{ "_id" : ObjectId("5cd9a118b50a6c6dd317ad99"), "ClientDetails" : [ { "ClientId" : 101, "ClientName" : "Chris" ... 阅读更多
63 次浏览
使用 $addToSet 在 MongoDB 中创建一个新字段。让我们首先创建一个包含文档的集合 -> db.createFieldDemo.insertOne({"StudentFirstName":"John", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99e28b50a6c6dd317ad95") } > db.createFieldDemo.insertOne({"StudentFirstName":"Larry", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99e2fb50a6c6dd317ad96") } > db.createFieldDemo.insertOne({"StudentFirstName":"Chris", "StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99e38b50a6c6dd317ad97") } > db.createFieldDemo.insertOne({"StudentFirstName":"David", "StudentAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99e43b50a6c6dd317ad98") }以下是使用 find() 方法显示集合中所有文档的查询 -> db.createFieldDemo.find().pretty();这将产生以下输出 -{ "_id" : ObjectId("5cd99e28b50a6c6dd317ad95"), "StudentFirstName" : ... 阅读更多
1K+ 次浏览
要将 MongoDB 结果存储在数组中,请使用 toArray() 方法 -var anyVariableName=db.yourCollectionName.find().toArray();让我们首先创建一个包含文档的集合 -> db.mongoDbResultInArrayDemo.insertOne({"CustomerName":"David Miller", "CustomerAge":24, "isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99bd5b50a6c6dd317ad92") } > db.mongoDbResultInArrayDemo.insertOne({"CustomerName":"Sam Williams", "CustomerAge":46, "isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99beab50a6c6dd317ad93") } > db.mongoDbResultInArrayDemo.insertOne({"CustomerName":"Carol Taylor", "CustomerAge":23, "isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99bf9b50a6c6dd317ad94") }以下是使用 find() 方法显示集合中所有文档的查询 -> db.mongoDbResultInArrayDemo.find().pretty();这将产生以下输出 -{ "_id" : ObjectId("5cd99bd5b50a6c6dd317ad92"), "CustomerName" : "David Miller", "CustomerAge" : ... 阅读更多
3K+ 次浏览
您可以使用 Map Reduce 的概念。让我们首先创建一个包含文档的集合 -> db.getAllFieldNamesDemo.insertOne({"StudentFirstName":"David", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd998e9b50a6c6dd317ad90") }以下是使用 find() 方法显示集合中所有文档的查询 -> db.getAllFieldNamesDemo.find();这将产生以下输出 ->{ "_id" : ObjectId("5cd998e9b50a6c6dd317ad90"), "StudentFirstName" : "David", "StudentAge" : 23 }以下是获取 MongoDB 集合中所有字段名称的查询 -> myMapReduce = db.runCommand({ "mapreduce" : "getAllFieldNamesDemo", "map" : function() { for (var myKey in this) { emit(myKey, null); } ... 阅读更多
426 次浏览
要使用前缀字符串更新数组中的所有元素,请使用 forEach()。让我们首先创建一个包含文档的集合 -> db.replaceAllElementsWithPrefixDemo.insertOne( { "StudentNames" : [ "John", "Carol" ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd91908b50a6c6dd317ad8e") } > > > db.replaceAllElementsWithPrefixDemo.insertOne( { "StudentNames" : [ "Sam" ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd9191cb50a6c6dd317ad8f") }以下是显示所有文档的查询 ... 阅读更多