找到 1349 篇文章 适用于 MongoDB
1K+ 次浏览
要添加 MD5 哈希值,请使用 hex_md5()。让我们首先创建一个包含文档的集合 ->db.addMd5HashValueDemo.insertOne({"UserName":"Adam", "UserPassword":"Adam123456"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6a4c66d78f205348bc619") } >db.addMd5HashValueDemo.insertOne({"UserName":"Chris", "UserPassword":"Chris_121#"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6a4e46d78f205348bc61a") }以下是在 find() 方法的帮助下显示集合中所有文档的查询 -> db.addMd5HashValueDemo.find();这将产生以下输出 -{ "_id" : ObjectId("5cd6a4c66d78f205348bc619"), "UserName" : "Adam", "UserPassword" : "Adam123456" } { "_id" : ObjectId("5cd6a4e46d78f205348bc61a"), "UserName" : "Chris", "UserPassword" : "Chris_121#" }以下是在 mongo 集合中添加 md5 哈希值的查询 -> db.addMd5HashValueDemo.find().forEach( function(documentPass){ documentPass.Value ... 阅读更多
1K+ 次浏览
要获取值的列表,请将 $group 聚合与 $push 运算符一起使用。让我们首先创建一个包含文档的集合 -> db.groupByDemo.insertOne({"UserName":"John", "Subject":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69f0457806ebf1256f136") } > db.groupByDemo.insertOne({"UserName":"Larry", "Subject":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69f0657806ebf1256f137") } > db.groupByDemo.insertOne({"UserName":"John", "Subject":"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69f0d57806ebf1256f138") } > db.groupByDemo.insertOne({"UserName":"John", "Subject":"C"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69f1357806ebf1256f139") } > db.groupByDemo.insertOne({"UserName":"Larry", "Subject":"SQL Server"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69f1c57806ebf1256f13a") }以下是在 find() 方法的帮助下显示集合中所有文档的查询 ... 阅读更多
86 次浏览
为此,请使用 $all 运算符。让我们首先创建一个包含文档的集合 ->db.findDocumentExactlySameInArrayDemo.insertOne({"TechnicalSubjects":["C++", "Java", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69a5f57806ebf1256f12e") } >db.findDocumentExactlySameInArrayDemo.insertOne({"TechnicalSubjects":["MySQL", "Java", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69ac057806ebf1256f12f") } >db.findDocumentExactlySameInArrayDemo.insertOne({"TechnicalSubjects":["C#", "Python", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69ad457806ebf1256f130") } >db.findDocumentExactlySameInArrayDemo.insertOne({"TechnicalSubjects":["MySQL", "C", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd69adf57806ebf1256f131") }以下是在 find() 方法的帮助下显示集合中所有文档的查询 -> db.findDocumentExactlySameInArrayDemo.find().pretty();这将产生以下输出 -{ "_id" : ObjectId("5cd69a5f57806ebf1256f12e"), "TechnicalSubjects" : [ ... 阅读更多
1K+ 次浏览
使用 $max 和 $min 运算符以及聚合框架来获取最大值和最小值。让我们首先创建一个包含文档的集合 -> db.maxAndMinDemo.insertOne({"Value":98}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698a357806ebf1256f129") } > db.maxAndMinDemo.insertOne({"Value":97}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698af57806ebf1256f12a") } > db.maxAndMinDemo.insertOne({"Value":69}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698b357806ebf1256f12b") } > db.maxAndMinDemo.insertOne({"Value":96}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698b657806ebf1256f12c") } > db.maxAndMinDemo.insertOne({"Value":99}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698b957806ebf1256f12d") }以下是在 find() 方法的帮助下显示集合中所有文档的查询 ... 阅读更多
229 次浏览
您可以为此使用 $addToSet 运算符。让我们首先创建一个包含文档的集合 -> db.insertDataIntoArrayDemo.insertOne( { "UserDetails":[ { "UserId" :"user121", "userGroupMessage":[] }, { "UserId" :"user221", "userGroupMessage":["Cool", "Good Morning"] } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd694e157806ebf1256f128") }以下是在 find() 方法的帮助下显示集合中所有文档的查询 ... 阅读更多
79 次浏览
要获取 MySQL LIKE 语句,请在 MongoDB 中使用 REGEX。让我们首先创建一个包含文档的集合 -> db.likeInMongoDBDemo.insertOne({"Name" : "Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6922857806ebf1256f123") } > db.likeInMongoDBDemo.insertOne({"Name" : "John" }); { "acknowledged" : true, "insertedId" : ObjectId("5cd6923157806ebf1256f124") } > db.likeInMongoDBDemo.insertOne({"Name" : "Scott"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6924557806ebf1256f125") } > db.likeInMongoDBDemo.insertOne({"Name" : "Sean"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6924f57806ebf1256f126") } > db.likeInMongoDBDemo.insertOne({"Name" : "Samuel"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6925857806ebf1256f127") }以下是在 find() 方法的帮助下显示集合中所有文档的查询 ... 阅读更多
613 次浏览
要合并,请使用 $setUnion 运算符。让我们首先创建一个包含文档的集合 -> db.mergeTwoArrayFieldDemo.insertOne({"NaturalNumbers":[1,2,3,8,10,20,30],"WholeNumbers":[0,1,2,63,78,20,45]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68e4057806ebf1256f11d") }以下是在 find() 方法的帮助下显示集合中所有文档的查询 -> db.mergeTwoArrayFieldDemo.find().pretty();这将产生以下输出 -{ "_id" : ObjectId("5cd68e4057806ebf1256f11d"), "NaturalNumbers" : [ 1, 2, 3, 8, 10, 20, 30 ], "WholeNumbers" : [ 0, 1, 2, 63, 78, 20, 45 ] }以下是在 MongoDB 中合并两个数组字段的查询。> db.mergeTwoArrayFieldDemo.aggregate([ { "$project": { "MergedArray": { "$setUnion": [ "$NaturalNumbers", "$WholeNumbers" ] } }} ]);这将产生以下输出 -{ "_id" : ObjectId("5cd68e4057806ebf1256f11d"), "MergedArray" : [ 0, 1, 2, 3, 8, 10, 20, 30, 45, 63, 78 ] }
84 次浏览
让我们首先创建一个包含文档的集合 -> db.findByObjectIdDemo.insertOne({"ClientName":"Larry", "ClientAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68cd657806ebf1256f11a") } > db.findByObjectIdDemo.insertOne({"ClientName":"Chris", "ClientAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68cdc57806ebf1256f11b") } > db.findByObjectIdDemo.insertOne({"ClientName":"David", "ClientAge":38, "isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68cf657806ebf1256f11c") }以下是在 find() 方法的帮助下显示集合中所有文档的查询 -> db.findByObjectIdDemo.find().pretty();这将产生以下输出 -{ "_id" : ObjectId("5cd68cd657806ebf1256f11a"), "ClientName" : "Larry", "ClientAge" : 23 } { "_id" : ObjectId("5cd68cdc57806ebf1256f11b"), "ClientName" : "Chris", "ClientAge" : 26 } { ... 阅读更多
271 次浏览
假设您已保存用户的登录日期。现在,您想要在特定日期之间登录的用户,即登录日期。为此,请将 $gte 和 $lt 运算符与 count() 一起使用。让我们首先创建一个包含文档的集合 -> db.findDataByDateDemo.insertOne({"UserName":"John", "UserLoginDate":new ISODate("2019-01-31")}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd8cd7bf3115999ed511ed") } > db.findDataByDateDemo.insertOne({"UserName":"Larry", "UserLoginDate":new ISODate("2019-02-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd8ce7bf3115999ed511ee") } > db.findDataByDateDemo.insertOne({"UserName":"Sam", "UserLoginDate":new ISODate("2019-05-02")}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd8cf3bf3115999ed511ef") } > db.findDataByDateDemo.insertOne({"UserName":"David", "UserLoginDate":new ISODate("2019-05-16")}); { "acknowledged" : true, "insertedId" : ObjectId("5cdd8d00bf3115999ed511f0") } > db.findDataByDateDemo.insertOne({"UserName":"Carol", ... 阅读更多
301 次浏览
您可以使用 $group 运算符和 _id: null。以下是语法 -db.yourCollectionName.aggregate([{$group: {_id:null, "anyFieldName": {$avg:"$yourFieldName"} } }]);让我们首先创建一个包含文档的集合 -> db.caculateTheAverageValueDemo.insertOne({"Population":100}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68a197924bb85b3f4895f") } > db.caculateTheAverageValueDemo.insertOne({"Population":500}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68a1c7924bb85b3f48960") } > db.caculateTheAverageValueDemo.insertOne({"Population":200}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68a237924bb85b3f48961") } > db.caculateTheAverageValueDemo.insertOne({"Population":100}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68a297924bb85b3f48962") } > db.caculateTheAverageValueDemo.insertOne({"Population":100}); { "acknowledged" : true, "insertedId" : ObjectId("5cd68a2e7924bb85b3f48963") }以下是使用 find() 函数显示集合中所有文档的查询... 阅读更多