找到关于 MongoDB 的1349 篇文章
830 次浏览
要计算时间戳差值,可以使用聚合框架。让我们首先创建一个包含文档的集合 -> db.timestampDifferenceDemo.insertOne({ "MovieBeginningTime": new ISODate("2019-05-12 10:20:30"), "MovieEndingTime":new ISODate("2019-05-12 12:30:20") }); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ba1f6d78f205348bc644") } > db.timestampDifferenceDemo.insertOne({ "MovieBeginningTime": new ISODate("2019-05-12 04:00:00"), "MovieEndingTime":new ISODate("2019-05-12 07:10:00") }); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ba3b6d78f205348bc645") }以下是使用 find() 方法显示集合中所有文档的查询 -> db.timestampDifferenceDemo.find().pretty();这将产生以下输出 -{ "_id" : ObjectId("5cd7ba1f6d78f205348bc644"), "MovieBeginningTime" : ISODate("2019-05-12T10:20:30Z"), "MovieEndingTime" : ISODate("2019-05-12T12:30:20Z") } { "_id" : ... 阅读更多
73 次浏览
是的,您可以使用 findOne()。以下是语法 -db.yourCollectionName.findOne();您也可以使用 toArray() -db.yourCollectionName.find().toArray();让我们首先创建一个包含文档的集合 -> db.betterFormatDemo.insertOne({"StudentName":"Adam Smith", "StudentScores":[98, 67, 89]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ab826d78f205348bc640") } > db.betterFormatDemo.insertOne({"StudentName":"John Doe", "StudentScores":[67, 89, 56]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ab936d78f205348bc641") } > db.betterFormatDemo.insertOne({"StudentName":"Sam Williams", "StudentScores":[45, 43, 33]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7aba76d78f205348bc642") }以下是使用 find() 方法显示集合中所有文档的查询 -> db.betterFormatDemo.find();这将产生以下输出 -{ "_id" : ObjectId("5cd7ab826d78f205348bc640"), "StudentName" ... 阅读更多
681 次浏览
让我们首先创建一个包含文档的集合 -> db.decrementingOperationDemo.insertOne({"ProductName":"Product-1", "ProductPrice":756}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7a8ae6d78f205348bc63c") } > db.decrementingOperationDemo.insertOne({"ProductName":"Product-2", "ProductPrice":890}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7a8b86d78f205348bc63d") } > db.decrementingOperationDemo.insertOne({"ProductName":"Product-3", "ProductPrice":994}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7a8c66d78f205348bc63e") } > db.decrementingOperationDemo.insertOne({"ProductName":"Product-4", "ProductPrice":1000}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7a8d06d78f205348bc63f") }以下是使用 find() 方法显示集合中所有文档的查询 -> db.decrementingOperationDemo.find().pretty();这将产生以下输出 -{ "_id" : ObjectId("5cd7a8ae6d78f205348bc63c"), "ProductName" : "Product-1", "ProductPrice" : 756 } { "_id" : ObjectId("5cd7a8b86d78f205348bc63d"), "ProductName" ... 阅读更多
114 次浏览
让我们首先创建一个包含文档的集合 -> db.matchingFirstItemInTheArrayDemo.insertOne( { "ClientDetails": [ { "ClientName": "Larry", "ClientAge":28 } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd7a5d26d78f205348bc636") } > db.matchingFirstItemInTheArrayDemo.insertOne( { "ClientDetails": [ { "ClientName": "Chris", "ClientAge":56, } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd7a5f56d78f205348bc637") } > db.matchingFirstItemInTheArrayDemo.insertOne( ... 阅读更多
687 次浏览
让我们首先创建一个包含文档的集合 -> db.excludeIdDemo.insertOne({"StudentFirstName":"John", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cd701a56d78f205348bc632") } > db.excludeIdDemo.insertOne({"StudentFirstName":"Robert", "StudentAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5cd701af6d78f205348bc633") } > db.excludeIdDemo.insertOne({"StudentFirstName":"Chris", "StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5cd701b86d78f205348bc634") }以下是使用 find() 方法显示集合中所有文档的查询 -> db.excludeIdDemo.find();这将产生以下输出 -{ "_id" : ObjectId("5cd701a56d78f205348bc632"), "StudentFirstName" : "John", "StudentAge" : 21 } { "_id" : ObjectId("5cd701af6d78f205348bc633"), "StudentFirstName" : "Robert", "StudentAge" : 20 } { "_id" : ObjectId("5cd701b86d78f205348bc634"), "StudentFirstName" : "Chris", "StudentAge" ... 阅读更多
289 次浏览
要连接字段,可以使用 $concat 运算符。让我们首先创建一个包含文档的集合 -> db.concatenateFieldsDemo.insertOne({"StudentFirstName":"Adam", "StudentLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ebf46d78f205348bc62e") } > db.concatenateFieldsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ebfc6d78f205348bc62f") } > db.concatenateFieldsDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ec376d78f205348bc630") } > db.concatenateFieldsDemo.insertOne({"StudentFirstName":"Sam", "StudentLastName":"Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ec436d78f205348bc631") }以下是使用 find() 方法显示集合中所有文档的查询 -> db.concatenateFieldsDemo.find().pretty();这将产生以下输出 -{ "_id" : ObjectId("5cd6ebf46d78f205348bc62e"), "StudentFirstName" : "Adam", "StudentLastName" : "Smith" } ... 阅读更多
189 次浏览
让我们首先创建一个包含文档的集合 -> 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();这将产生以下输出 -{ "_id" : ObjectId("5cd9e9f9b50a6c6dd317adb3"), "Scores" : [ ... 阅读更多
489 次浏览
为此,可以使用 $hour 运算符。让我们首先创建一个包含日期字段的文档集合 -> db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry", "OrderDatetime":new ISODate("2019-01-31 09:45:50")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6e8a86d78f205348bc62a") } > db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry", "OrderDatetime":new ISODate("2019-02-21 01:10:01")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6e8b86d78f205348bc62b") } > db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry", "OrderDatetime":new ISODate("2019-04-01 04:10:11")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6e8e26d78f205348bc62c") } > db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry", "OrderDatetime":new ISODate("2019-05-11 08:53:01")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6e8f26d78f205348bc62d") }以下是使用 find() 方法显示集合中所有文档的查询 -> db.mongoDbSearchForHoursDemo.find().pretty();这将 ... 阅读更多
341 次浏览
使用 initializeUnorderedBulkOp() 的概念。让我们首先创建一个包含文档的集合:->db.upDateMultipleRowsDemo.insertOne({"CustomerName":"John", "CustomerPurchaseAmount":500}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ceb06d78f205348bc626") } >db.upDateMultipleRowsDemo.insertOne({"CustomerName":"Chris", "CustomerPurchaseAmount":700}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ceb26d78f205348bc627") } >db.upDateMultipleRowsDemo.insertOne({"CustomerName":"David", "CustomerPurchaseAmount":50}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ceb36d78f205348bc628") } >db.upDateMultipleRowsDemo.insertOne({"CustomerName":"Larry", "CustomerPurchaseAmount":1900}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6ceb46d78f205348bc629") }以下是使用 find() 方法显示集合中所有文档的查询:-> db.upDateMultipleRowsDemo.find().pretty();这将产生以下输出:{ "_id" : ObjectId("5cd6ceb06d78f205348bc626"), "CustomerName" : "John", "CustomerPurchaseAmount" : 500 } { ... 阅读更多
129 次浏览
使用 MongoDB 中的 $or 运算符计算一个或多个表达式。以下是语法:db.yourCollectionName.find({ $or: [{ "yourFieldName": yourValue1 }, { "yourFieldName": yourValue2} ] } ).pretty();让我们首先创建一个包含文档的集合:-> db.orOperatorDemo.insertOne({"StudentNames":["John", "Carol", "Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6b80a6d78f205348bc61b") } > db.orOperatorDemo.insertOne({"StudentNames":["Robert", "Chris", "David"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6b8266d78f205348bc61c") } > db.orOperatorDemo.insertOne({"StudentNames":["John"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6b8346d78f205348bc61d") }以下是使用 find() 方法显示集合中所有文档的查询:-> db.orOperatorDemo.find().pretty();这将产生以下输出:{ "_id" ... 阅读更多