找到关于 MongoDB 的1349 篇文章
2K+ 次浏览
在 MongoDB 中,不存在用于将集合从一个数据库复制到另一个数据库的命令。要实现此目的,请使用以下概念:db.yourCollectionName.find().forEach(function(yourVariableName){ db.getSiblingDB('yourDestinationDatabase')['yourCollectionName'].insert(yourVariableName); });让我们在测试数据库中创建一个集合,并将此集合复制到另一个名为“sample”的数据库。为了理解上述语法,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:> use test 切换到数据库 test > db.copyThisCollectionToSampleDatabaseDemo.insertOne({"User_Id":101, "UserName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77ad622386c62d05142a67") } > db.copyThisCollectionToSampleDatabaseDemo.insertOne({"User_Id":102, "UserName":"Maxwell"}); { "acknowledged" : true, ... 阅读更多
192 次浏览
如果要列出特定数据库中的所有集合,则需要先切换数据库。查询如下:> use sample; 切换到数据库 sample > db.getCollectionNames();以下是输出:[ "copyThisCollectionToSampleDatabaseDemo", "deleteDocuments", "deleteDocumentsDemo", "deleteInformation", "employee", "internalArraySizeDemo", "sourceCollection", "updateInformation", "userInformation" ]另一种查询如下:> show collections;以下是输出:copyThisCollectionToSampleDatabaseDemo deleteDocuments deleteDocumentsDemo deleteInformation employee internalArraySizeDemo sourceCollection updateInformation userInformation
337 次浏览
要按升序排序,语法如下:db.yourCollectionName.find().sort({yourField:1});为了理解这个概念,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:> db.sortingDemo.insertOne({"Value":100}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e2ed3c9d04998abf006") } > db.sortingDemo.insertOne({"Value":1}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e31d3c9d04998abf007") } > db.sortingDemo.insertOne({"Value":150}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e34d3c9d04998abf008") } > db.sortingDemo.insertOne({"Value":250}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e37d3c9d04998abf009") } > db.sortingDemo.insertOne({"Value":5}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e3bd3c9d04998abf00a") } > db.sortingDemo.insertOne({"Value":199}); { "acknowledged" : true, ... 阅读更多
198 次浏览
要按升序排序,语法如下:db.yourCollectionName.find().sort({yourField:1});为了理解这个概念,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:> db.sortingDemo.insertOne({"Value":100}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e2ed3c9d04998abf006") } > db.sortingDemo.insertOne({"Value":1}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e31d3c9d04998abf007") } > db.sortingDemo.insertOne({"Value":150}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e34d3c9d04998abf008") } > db.sortingDemo.insertOne({"Value":250}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e37d3c9d04998abf009") } > db.sortingDemo.insertOne({"Value":5}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e3bd3c9d04998abf00a") } > db.sortingDemo.insertOne({"Value":199}); { "acknowledged" : true, ... 阅读更多
391 次浏览
您可以在 MongoDB 中使用 distinct() 方法来获取不同的记录值。语法如下:db.yourCollectionName.distinct(“yourFieldName”);为了理解上述语法,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:> db.distinctRecordDemo.insertOne({"StudentId":1, "StudentName":"John", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c77a78299b97a65744c1b50") } > db.distinctRecordDemo.insertOne({"StudentId":2, "StudentName":"John", "StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c77a78b99b97a65744c1b51") } > db.distinctRecordDemo.insertOne({"StudentId":3, "StudentName":"Carol", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c77a79a99b97a65744c1b52") } > db.distinctRecordDemo.insertOne({"StudentId":4, "StudentName":"Carol", "StudentAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c77a7a499b97a65744c1b53") } > db.distinctRecordDemo.insertOne({"StudentId":5, "StudentName":"Sam", "StudentAge":24}); ... 阅读更多
243 次浏览
您可以借助聚合框架实现此目的。为了理解这个概念,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:> db.countGroupByDemo.insertOne({"StudentId":10, "StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7700871e9c5dd6f1f78296") } > db.countGroupByDemo.insertOne({"StudentId":10, "StudentName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77008f1e9c5dd6f1f78297") } > db.countGroupByDemo.insertOne({"StudentId":20, "StudentName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7700971e9c5dd6f1f78298") } > db.countGroupByDemo.insertOne({"StudentId":30, "StudentName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7700a21e9c5dd6f1f78299") } > db.countGroupByDemo.insertOne({"StudentId":30, "StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7700aa1e9c5dd6f1f7829a") } ... 阅读更多
2K+ 次浏览
您可以为此使用 $ne(不等于)运算符。为了理解这个概念,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:> db.arrayFieldIsNotEmptyDemo.insertOne({"StudentName":"Larry", "StudentTechnicalSubject":["Java", "C"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c76fe2f1e9c5dd6f1f78291") } > db.arrayFieldIsNotEmptyDemo.insertOne({"StudentName":"Mike", "StudentTechnicalSubject":[]}); { "acknowledged" : true, "insertedId" : ObjectId("5c76fe3b1e9c5dd6f1f78292") } > db.arrayFieldIsNotEmptyDemo.insertOne({"StudentName":"Sam", "StudentTechnicalSubject":["MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c76fe491e9c5dd6f1f78293") } > db.arrayFieldIsNotEmptyDemo.insertOne({"StudentName":"Carol", "StudentTechnicalSubject":[]}); { "acknowledged" : true, "insertedId" : ObjectId("5c76fe521e9c5dd6f1f78294") } > db.arrayFieldIsNotEmptyDemo.insertOne({"StudentName":"David", "StudentTechnicalSubject":["MySQL", "SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c76fe661e9c5dd6f1f78295") }显示所有文档 ... 阅读更多
185 次浏览
您可以使用 `$exists` 和 `$ne` 运算符来实现这一点。为了更好地理解这个概念,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:-> db.checkFieldExistDemo.insertOne({"EmployeeId":1, "EmployeeName":"John", "isMarried":true, "EmployeeSalary":4648585}); { "acknowledged" : true, "insertedId" : ObjectId("5c76f7b31e9c5dd6f1f78281") } > db.checkFieldExistDemo.insertOne({"StudentId":2, "StudentName":"John", "isMarried":false, " StudentAge":19}); { "acknowledged" : true, 0 "insertedId" : ObjectId("5c76f7e11e9c5dd6f1f78282") } 使用 `find()` 方法显示集合中的所有文档。查询如下:-> db.checkFieldExistDemo.find().pretty(); 输出 { "_id" : ObjectId("5c76f7b31e9c5dd6f1f78281"), "EmployeeId" : 1, "EmployeeName" : "John", "isMarried" : true, ... 阅读更多
342 次浏览
您可以使用 `$sort` 运算符和 `$group` 语句来获取集合中每个分组的最大属性文档。为了更好地理解这个概念,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:-> db.maxAttributePerGroup.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith ", "StudentAge":29, "StudentId":10}); { "acknowledged" : true, "insertedId" : ObjectId("5c76ee341e9c5dd6f1f78277") } > db.maxAttributePerGroup.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Taylo r", "StudentAge":19, "StudentId":10}); { "acknowledged" : true, "insertedId" : ObjectId("5c76ee4e1e9c5dd6f1f78278") } > db.maxAttributePerGroup.insertOne({"StudentFirstName":"Adam", "StudentLastName":"Smit h", "StudentAge":34, "StudentId":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c76ee631e9c5dd6f1f78279") } > db.maxAttributePerGroup.insertOne({"StudentFirstName":"Bob", "StudentLastName":"Taylor ", "StudentAge":58, "StudentId":20}); { ... 阅读更多
907 次浏览
为了在MongoDB文档中替换子字符串,您可以使用 `replace()` 函数。为了更好地理解它,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:-> db.replaceSubstringDemo.insertOne({"WebsiteURL":"www.gogle.com"}); { "acknowledged" : true, "insertedId" : ObjectId("5c76eaf21e9c5dd6f1f78276") } 使用 `find()` 方法显示集合中的所有文档。查询如下:-> db.replaceSubstringDemo.find().pretty(); 输出 { "_id" : ObjectId("5c76eaf21e9c5dd6f1f78276"), "WebsiteURL" : "www.gogle.com" } 这是在MongoDB文档中替换子字符串的查询:-> db.replaceSubstringDemo.find({WebsiteURL:"www.gogle.com"}).forEach(function(url, k){ ... url.WebsiteURL=url.WebsiteURL.replace("www.gogle.com", "www.google.com"); ... db.replaceSubstringDemo.save(url) ... });让我们显示... 阅读更多