找到關於 MongoDB 的1349 篇文章

如何在 MongoDB 中根據索引刪除陣列元素?

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

1K+ 次瀏覽

要根據索引刪除 MongoDB 中的陣列元素,您可以使用 `$unset` 和 `$pull` 運算子。從陣列中刪除陣列元素分兩步進行。語法如下:`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 中的陣列元素,您可以使用 `$pull` 和 `$in` 運算子。語法如下:`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" }以下是... 閱讀更多

廣告