找到關於 MongoDB 的1349 篇文章

1K+ 次瀏覽
MongoDB 的介紹。mongodb.connect 此方法用於將 MongoDB 伺服器與我們的 Node 應用程式連線。這是 MongoDB 模組中的非同步方法。語法 mongodb.connect(path[, callback]) 引數 • path – MongoDB 伺服器實際執行的伺服器路徑及其埠。• callback – 如果發生任何錯誤,此函式將提供回撥。安裝 Mongo-DB 在繼續嘗試將應用程式與 Nodejs 連線之前,我們需要首先設定我們的 MongoDB 伺服器。使用以下查詢從 npm 安裝 mongoDB。npm install mongodb –save 執行以下命令以在特定的本地主機伺服器上設定您的 mongoDB。這將有助於建立與…的連線 閱讀更多

601 次瀏覽
MongoDB 是一種廣泛使用的文件資料庫,也是一種 NoSQL 資料庫。Python 可以透過一些 Python 模組與 MongoDB 互動,並在 MongoDB 中建立和操作資料。在本文中,我們將學習如何做到這一點。但在 Python 可以連線到 MongoDB 並執行之前,您的系統中應該已經安裝了 MongoDB。要在您的系統中安裝 MongoDB,請訪問我們的 MongoDB 教程。安裝 pymongo 要與 MongoDB 互動,我們需要名為 pymongo 的模組。使用以下命令在您的 Python 環境中安裝它。pip install pymogo 檢查現有資料庫 我們現在使用此 Python 模組… 閱讀更多

658 次瀏覽
要聚合多個結果,請在 MongoDB 中使用 $group。讓我們建立一個包含文件的集合 −> db.demo765.insertOne( ... ... { ... Name:"John", ... "Category":"ComputerScience", ... "SubjectName":"MongoDB", ... "Marks":75 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eb054525637cd592b2a4b01") } > > db.demo765.insertOne( ... { ... Name:"John", ... "Category":"ComputerScience", ... "SubjectName":"MySQL", ... "Marks":85 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eb054525637cd592b2a4b02") } > db.demo765.insertOne( ... ... 閱讀更多

706 次瀏覽
使用 $ne 檢查非空。讓我們建立一個包含文件的集合 −> db.demo764.insertOne({"LoginUserName":"Chris", "LoginPassword":"Chris_12"}); { "acknowledged" : true, "insertedId" : ObjectId("5eb04ee55637cd592b2a4afc") } > db.demo764.insertOne({"LoginUserName":"Chris", "LoginPassword":null}); { "acknowledged" : true, "insertedId" : ObjectId("5eb04eee5637cd592b2a4afd") } > db.demo764.insertOne({"LoginUserName":"Chris", "LoginPassword":""}); { "acknowledged" : true, "insertedId" : ObjectId("5eb04ef35637cd592b2a4afe") } 使用 find() 方法顯示集合中的所有文件 −> db.demo764.find(); 這將產生以下輸出 −{ "_id" : ObjectId("5eb04ee55637cd592b2a4afc"), "LoginUserName" : "Chris", "LoginPassword" : "Chris_12" } { "_id" : ObjectId("5eb04eee5637cd592b2a4afd"), "LoginUserName" : "Chris", "LoginPassword" : null } { "_id" : ObjectId("5eb04ef35637cd592b2a4afe"), "LoginUserName" : "Chris", ... 閱讀更多

1K+ 次瀏覽
要查詢物件陣列以獲取巢狀文件,請使用 find()。讓我們建立一個包含文件的集合 −> db.demo763.insertOne( ... { ... _id:1, ... CountryName:"US", ... "studentInformation": [ ... { ... StudentName:"Chris", ... }, ... { ... StudentName:"David", ... StudentAge:22 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 1 } 使用 find() 方法顯示集合中的所有文件 −> db.demo763.find(); 這將產生以下輸出 −{ "_id" : 1, "CountryName" : "US", "studentInformation" : [ { "StudentName" : "Chris" }, { "StudentName" : "David", "StudentAge" : 22 } ] } 下面是如何查詢物件陣列以獲取特定巢狀文件 −> db.demo763.find({}, ... { ... studentInformation: { ... $elemMatch: { ... StudentAge: { ... $exists: true ... } ... } ... } ... }) 這將產生以下輸出 −{ "_id" : 1, "studentInformation" : [ { "StudentName" : "David", "StudentAge" : 22 } ] }

1K+ 次瀏覽
為此,請使用 $project 與 aggregate() 一起使用。聚合中的 $project 將包含所需欄位的文件傳遞到管道的下一階段。讓我們建立一個包含文件的集合 −> db.demo762.insertOne({ ... "_id" : { ... "userId":101, ... "userName":"Chris" ... }, .. . "countryName" : "US", ... ... "details" : [ ... { ... "Name" : "Robert", ... "DueDate" : "2020-04-10" ... ... }, ... ... { ... ... 閱讀更多

556 次瀏覽
要從陣列中獲取特定值,請使用 aggregate() 與 $project 一起使用。讓我們建立一個包含文件的集合 −> db.demo761.insertOne( ... { ... "details": [ ... { ... "student": { ... "FullName": "Chris Brown" ... } ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eb034d15637cd592b2a4af1") } > db.demo761.insertOne( ... { ... ... 閱讀更多

507 次瀏覽
要使用單個查詢更新多個文件,請在 MongoDB 中使用 bulkWrite()。讓我們建立一個包含文件的集合 −> db.demo760.insertOne({id:1, details:{Value1:100, Value2:50}}); { "acknowledged" : true, "insertedId" : ObjectId("5eb0309f5637cd592b2a4aee") } > db.demo760.insertOne({id:2, details:{Value1:60, Value2:70}}); { "acknowledged" : true, "insertedId" : ObjectId("5eb030a15637cd592b2a4aef") } > db.demo760.insertOne({id:3, details:{Value1:80, Value2:90}}); { "acknowledged" : true, "insertedId" : ObjectId("5eb030a15637cd592b2a4af0") } 使用 find() 方法顯示集合中的所有文件 −> db.demo760.find(); 這將產生以下輸出 −{ "_id" : ObjectId("5eb0309f5637cd592b2a4aee"), "id" : 1, "details" : { "Value1" : 100, "Value2" : 50 } } { "_id" : ... 閱讀更多

505 次瀏覽
為此,您需要使用不區分大小寫的 (i)。讓我們建立一個包含文件的集合 −> db.demo759.insertOne({SubjectName:"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5eb02ba95637cd592b2a4ae7") } > db.demo759.insertOne({SubjectName:"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5eb02baa5637cd592b2a4ae8") } > db.demo759.insertOne({SubjectName:"mongodb"}); { "acknowledged" : true, "insertedId" : ObjectId("5eb02baf5637cd592b2a4ae9") } > db.demo759.insertOne({SubjectName:"MONGODB"}); { "acknowledged" : true, "insertedId" : ObjectId("5eb02bb85637cd592b2a4aea") }使用 find() 方法顯示集合中的所有文件 −> db.demo759.find();這將產生以下輸出 −{ "_id" : ObjectId("5eb02ba95637cd592b2a4ae7"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5eb02baa5637cd592b2a4ae8"), "SubjectName" : "MongoDB" } { "_id" : ... 閱讀更多

457 次瀏覽
為此,您可以使用 $out 與 aggregate() 和 $unwind 一起使用。讓我們建立一個包含文件的集合 −> db.demo757.insertOne( ... { ... "id": 101, ... "Name": ["John", "Bob", "Chris"] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eb025745637cd592b2a4ae2") } > db.demo757.insertOne( ... { ... "id": 102, ... "Name": ["David"] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eb025755637cd592b2a4ae3") }使用 find() 方法顯示集合中的所有文件 −> db.demo757.find();這將產生 ... 閱讀更多