找到 1660 篇文章 关于大数据分析
2K+ 阅读量
您可以使用 $regex 运算符来检查 MongoDB 中的字段是否包含某个字符串。语法如下:db.yourCollectionName.findOne({"yourFieldName":{$regex:".*yourValue.*"}});为了理解上述概念,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:> db.checkFieldContainsStringDemo.insertOne({"Id":1, "Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d762fc4e719b197a12ed") } > db.checkFieldContainsStringDemo.insertOne({"Id":2, "Name":"Johnson"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d76bfc4e719b197a12ee") } > db.checkFieldContainsStringDemo.insertOne({"Id":3, "Name":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d774fc4e719b197a12ef") } > db.checkFieldContainsStringDemo.insertOne({"Id":4, "Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d77cfc4e719b197a12f0") } > db.checkFieldContainsStringDemo.insertOne({"Id":5, "Name":"Sam"}); ... 阅读更多
2K+ 阅读量
要在 MongoDB 中查找长度大于特定值的字符串,请使用 $where 运算符。语法如下:db.yourCollectionName.find({$where:'this.yourStringFieldName.length > yourIntegerValue'}).pretty();为了理解上述概念,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:> db.stringFieldLengthDemo.insertOne({"UserId":1, "UserName":"Adam Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb4b2386c62d05142a78") } > db.stringFieldLengthDemo.insertOne({"UserId":2, "UserName":"Carol Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb562386c62d05142a79") } > db.stringFieldLengthDemo.insertOne({"UserId":3, "UserName":"James Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb5b2386c62d05142a7a") } > db.stringFieldLengthDemo.insertOne({"UserId":4, "UserName":"John Smith"}); { "acknowledged" : ... 阅读更多
32K+ 阅读量
要在 MongoDB 中搜索对象数组,可以使用 $elemMatch 运算符。此运算符允许我们从对象数组中搜索多个组件。为了理解上述概念,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:> db.searchArrayDemo.insertOne({"EmployeeFirstName":"Adam", "EmployeeLastName":"Smith", "EmployeeDateOfBirth":new ISODate("1992-01-31 13:45:10"), ... "EmployeeSkills":["Spring and Hibernate Framework", "Machine Learning"], ... "EmployeeDetails":[ ... { ... "EmployeePerformanceArea":"Java", ... "Year":2001 ... }, ... { ... ... 阅读更多
2K+ 阅读量
在 MongoDB 中,不存在将一个数据库中的集合复制到另一个数据库的命令。要实现此目的,请使用以下概念:db.yourCollectionName.find().forEach(function(yourVariableName){ db.getSiblingDB('yourDestinationDatabase')['yourCollectionName'].insert(yourVariableName); });让我们在 test 数据库中创建一个集合,并将此集合复制到另一个名为“sample”的数据库。为了理解上述语法,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:> use test switched to db test > db.copyThisCollectionToSampleDatabaseDemo.insertOne({"User_Id":101, "UserName":"Larr y"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77ad622386c62d05142a67") } > db.copyThisCollectionToSampleDatabaseDemo.insertOne({"User_Id":102, "UserName":"Maxwell"}); { "acknowledged" : true, ... 阅读更多
192 阅读量
如果要列出特定数据库中的所有集合,则需要先切换到该数据库。查询如下:> use sample; switched to db 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" : ... 阅读更多
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" : ... 阅读更多
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") }显示所有文档 ... 阅读更多