Processing math: 100%

找到关于 MongoDB 的1349 篇文章

如何在 MongoDB 中根据索引删除数组元素?

Nancy Den
更新于 2019年7月30日 22:30:25

1K+ 次浏览

要根据索引删除 MongoDB 中的数组元素,您可以使用 `unsetpull` 运算符。从数组中删除数组元素分两步进行。语法如下:`db.yourCollectionName.update({}, {unset:{"yourArrayListName.yourPosition":yourPositionValue}}; db.yourCollectionName.update({}, {pull:{"yourArrayListName":null}});` 为了理解上述语法,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:`>db.removeArrayElements.insertOne({"StudentName":"Larry", "StudentAge":23, "TechnicalSub ject":["C", "C++", "Java", "MongoDB"]});` { "acknowledged" : true, "insertedId" : ObjectId("5c6ea4879c4643706aef56d2") } 显示集合中所有文档,可以使用 `find()` 方法。查询如下:`> db.removeArrayElements.find().pretty();` 输出如下:{ "_id" : ObjectId("5c6ea4879c4643706aef56d2"), ... 阅读更多

在 MongoDB 中存储日期/时间的最佳方法?

Daniol Thomas
更新于 2019年7月30日 22:30:25

1K+ 次浏览

您可以通过两种不同的方法在 MongoDB 中存储日期/时间。在第一种方法中,您可以像 JavaScript 一样使用 Date 对象。Date 对象是存储 MongoDB 中日期/时间的最佳方法。语法如下:`new Date();` 在第二种方法中,您可以使用 ISODate()。语法如下:`new ISODate();` 为了理解上述语法,让我们按照第一种方法创建一个包含文档的集合。创建包含文档的集合的查询如下:第一种方法:`> db.ProductsInformation.insertOne({"ProductId":"Product-1", "ProductDeliveryDateTime":new Date()});` { "acknowledged" : true, "insertedId" : ObjectId("5c6ec6786fd07954a4890686") } 第二种方法:`> db.ProductsInformation.insertOne({"ProductId":"Product-2", "ProductDeliveryDateTime":new ... 阅读更多

如何更新 MongoDB 文档的 _id?

Daniol Thomas
更新于 2019年7月30日 22:30:25

10K+ 次浏览

您无法更新它,但您可以保存一个新的 ID 并删除旧的 ID。为了更新 MongoDB 的 _id,请按照以下步骤操作:步骤 1:第一步,您需要将 ObjectId 存储到变量中。`anyVariableName=db.yourCollectionName.findOne({_id:yourObjectIdValue)});` 步骤 2:第二步,您需要设置一个新的 ID。`yourDeclaredVariableName._id=yourNewObjectIdValue;` 步骤 3:第三步,您需要在文档上插入新的 ID。`db.yourCollectionName.insert(yourDeclaredVariableName);` 步骤 4:第四步,您需要删除旧的 ID。`db.yourCollectionName.remove({_id:yourOldObjectIdValue)});` 为了理解上述步骤,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下: ... 阅读更多

计算 MongoDB 中数组中项目的数量?

Daniol Thomas
更新于 2019年7月30日 22:30:25

5K+ 次浏览

要计算数组中项目的数量,您可以使用 `size` 运算符。语法如下:`db.yourCollectionName.aggregate({project:{anyFieldName:{size:"yourArrayName"}}}).prett y();` 为了理解上述语法,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:`>db.getSizeOfArray.insertOne({"StudentId":1, "StudentName":"Larry", "StudentMarks":[87, 34, 5 6, 77, 89, 90]});` { "acknowledged" : true, "insertedId" : ObjectId("5c6ebc536fd07954a4890680") } `>db.getSizeOfArray.insertOne({"StudentId":2, "StudentName":"Sam", "StudentMarks":[90, 76, 56 ]});` { "acknowledged" : true, "insertedId" : ObjectId("5c6ebc6b6fd07954a4890681") } `>db.getSizeOfArray.insertOne({"StudentId":3, "StudentName":"Carol", "StudentMarks":[90, 76]}) ;` { "acknowledged" : true, "insertedId" : ObjectId("5c6ebc7a6fd07954a4890682") } 现在您可以显示集合中的所有文档 ... 阅读更多

如何在 MongoDB 中选择单个字段?

Daniol Thomas
更新于 2023年9月14日 15:35:43

28K+ 次浏览

您可以使用以下语法在 MongoDB 中选择单个字段:`db.yourCollectionName.find({"yourFieldName":yourValue}, {"yourSingleFieldName":1, _id:0});` 在上述语法中,`"yourSingleFieldName":1, _id:0` 表示获取一个字段中的所有数据,而无需 _id。为了理解上述语法,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:`> db.singleFieldDemo.insertOne({"StudentName":"David", "StudentAge":28});` { "acknowledged" : true, "insertedId" : ObjectId("5c6eba356fd07954a489067c") } `> db.singleFieldDemo.insertOne({"StudentName":"Bob", "StudentAge":18});` { "acknowledged" : true, "insertedId" : ObjectId("5c6eba406fd07954a489067d") } `> db.singleFieldDemo.insertOne({"StudentName":"Chris", "StudentAge":24});` { "acknowledged" : true, "insertedId" : ObjectId("5c6eba4c6fd07954a489067e") } `> db.singleFieldDemo.insertOne({"StudentName":"Robert", "StudentAge":26});` { "acknowledged" : true, "insertedId" : ObjectId("5c6eba586fd07954a489067f") ... 阅读更多

从 MongoDB 中的数组中删除对象?

Daniol Thomas
更新于 2019年7月30日 22:30:25

1K+ 次浏览

要从 MongoDB 中的数组中删除对象,您可以使用 `pull` 运算符。语法如下:`db.yourCollectionName.update( {'_id':ObjectId("5c6ea036a0c51185aefbd14f")}, {pull:{"yourArrayName":{"yourArrayFieldName":yourValue}}}, false, true);` 为了理解上述语法,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:`> db.removeObject.insertOne({"CustomerName":"Maxwell", "CustomerAge":23, ... "CustomerDetails":[ ... { ... "CustomerId":100, ... "CustomerProduct":"Product-1" ... }, ... { ... "CustomerId":150, ... "CustomerProduct":"Product-2" ... }, ... { ... "CustomerId":200, ... "CustomerProduct":"Product-3" ... } ... ] ... });` { "acknowledged" : true, "insertedId" : ObjectId("5c6ea036a0c51185aefbd14f") } 使用 `find()` 方法显示集合中的所有文档。查询如下: ... 阅读更多

如何在 MongoDB 中删除数组元素?

Krantik Chavan
更新于 2019年7月30日 22:30:25

752 次浏览

要删除 MongoDB 中的数组元素,您可以使用 `pullin` 运算符。语法如下:`db.yourCollectionName.update({}, {pull:{yourFirstArrayName:{in:["yourValue"]}, yourSecondArrayName:"yourValue"}}, {multi:true} );` 为了理解上述语法,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:`>db.removeArrayElement.insertOne({"StudentName":"Larry", "StudentCoreSubject":["MongoD B", "MySQL", "SQL Server", "Java"], "StudentFavouriteTeacher":["John", "Marry", "Carol"]});` { "acknowledged" : true, "insertedId" : ObjectId("5c6ec9c46fd07954a4890688") } 使用 `find()` 方法显示集合中的所有文档。查询如下:`> db.removeArrayElement.find().pretty();` 输出如下:{ "_id" : ObjectId("5c6ec9c46fd07954a4890688"), "StudentName" : "Larry", "StudentCoreSubject" : [ ... 阅读更多

在 MongoDB 中查找包含特定值的数组的文档

Krantik Chavan
更新于 2019年7月30日 22:30:25

749 次浏览

可以使用 find() 方法查找包含特定值的数组的文档。语法如下:db.yourCollectionName.find({"yourArrayFieldName":"yourValue"}, .......N).pretty();为了理解上述语法,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下:>db.findSpecificValue.insertOne({"StudentId":1, "StudentName":"Larry", "FavouriteSubject":["C", "C++", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6e8996140577d89182b8d0") } >db.findSpecificValue.insertOne({"StudentId":2, "StudentName":"Larry", "FavouriteSubject":["MongoDB", "MySQL", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6e89b1140577d89182b8d1") }使用 find() 方法显示集合中的所有文档。查询如下:> db.findSpecificValue.find().pretty();以下是输出:{    "_id" : ObjectId("5c6e8996140577d89182b8d0"),    "StudentId" ... 阅读更多

如何在MongoDB中更新精确的数组元素中的字段?

Krantik Chavan
更新于 2019年7月30日 22:30:25

浏览量:231

您可以使用以下语句在MongoDB中更新精确的数组元素。语法如下:{"yourArrayDocumentName.$.yourNestedArrayDocument.yourPosition":"yourValue"}});为了理解上述语法,让我们创建一个包含一些文档的集合。创建包含文档的集合的查询如下:> db.updateExactField.insertOne({"ActorId":1, "ActorDetails":[{"ActorName":"Johnny Depp", "MovieList": ["The Tourist", "Public Enemy"]}, ... {"ActorName":"Chris Evans", "MovieList":["Captain America", "Avengers"]}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6d7f63f2db199c1278e7f1") }现在您可以使用 find() 方法显示集合中的文档。查询如下:> db.updateExactField.find().pretty();以下是输出:{    "_id" : ObjectId("5c6d7f63f2db199c1278e7f1"),    "ActorId" : 1,   ... 阅读更多

MongoDB中是否可以进行不区分大小写的查询?

Krantik Chavan
更新于 2019年7月30日 22:30:25

浏览量:335

是的,您可以使用正则表达式在MongoDB中进行不区分大小写的查询。语法如下:db.yourCollectionName.find({"yourFieldName":/^yourvalue$/i});为了理解上述语法,让我们创建一个包含一些文档的集合。创建包含文档的集合的查询如下:> db.caseInsensitiveDemo.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6d7a67f2db199c1278e7ef") } > db.caseInsensitiveDemo.insertOne({"Name":"JOHN"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6d7ad6f2db199c1278e7f0") }使用 find() 显示集合中的所有文档。查询如下:> db.caseInsensitiveDemo.find();以下是输出:{ "_id" : ObjectId("5c6d7a67f2db199c1278e7ef"), "Name" : "John" } { "_id" : ObjectId("5c6d7ad6f2db199c1278e7f0"), "Name" : "JOHN" }以下是... 阅读更多

广告