找到 1660 篇文章 關於大資料分析
513 次瀏覽
您可以使用聚合框架在 MongoDB 中按日期分組。讓我們首先建立一個包含一些文件的集合。建立包含文件的集合的查詢如下:> db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate()}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ee4df6fd07954a4890695") } > db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate("2019-01-31 15:20:09.234Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ee51c6fd07954a4890696") } > db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate("2017-04-21 16:12:13.240Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ee5336fd07954a4890697") } > db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate("2016-05-25 19:11:21.130Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ee54b6fd07954a4890698") } > db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate("2016-05-25 19:11:21.130Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ee8de6fd07954a4890699") } > ... 閱讀更多
2K+ 次瀏覽
讓我們以將字串型別轉換為整數為例。聚合不允許我們直接更改欄位的型別;因此,您需要編寫程式碼來轉換欄位的型別。首先,建立一個包含文件的集合。之後我們將獲取每個欄位的型別。建立包含文件的集合的查詢如下>db.changeDataType.insertOne({"StudentName":"Larry", "StudentAge":23, "StudentZipCode":" 10001", "isProgrammer":false}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ed4976fd07954a4890694") }使用 find() 方法顯示集合中的所有文件。查詢如下:> db.changeDataType.find().pretty();輸出如下:{ ... 閱讀更多
1K+ 次瀏覽
要在 MongoDB 中獲取最後 N 條記錄,您需要使用 limit()。語法如下:db.yourCollectionName.find().sort({$natural:-1}).limit(yourValue);為了理解上述語法,讓我們建立一個包含文件的集合。建立包含文件的集合的查詢如下:> db.getLastNRecordsDemo.insertOne({"EmployeeName":"Maxwell"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ecf3d6fd07954a4890689") } > db.getLastNRecordsDemo.insertOne({"EmployeeName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ecf496fd07954a489068a") } > db.getLastNRecordsDemo.insertOne({"EmployeeName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ecf4e6fd07954a489068b") } > db.getLastNRecordsDemo.insertOne({"EmployeeName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ecf546fd07954a489068c") } > db.getLastNRecordsDemo.insertOne({"EmployeeName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ecf596fd07954a489068d") } > db.getLastNRecordsDemo.insertOne({"EmployeeName":"Mike"}); ... 閱讀更多
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"), ... 閱讀更多
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 ... 閱讀更多
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)});為了理解上述步驟,讓我們建立一個包含文件的集合。建立包含文件的集合的查詢 ... 閱讀更多
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") }現在您可以從集合中顯示所有文件 ... 閱讀更多
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") ... 閱讀更多
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() 方法顯示集合中的所有文件。查詢如下 ... 閱讀更多
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" : [ ... 閱讀更多
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP