找到 1349 篇文章 關於 MongoDB

256 次瀏覽
您可以使用投影運算子 $elemMatch 在 MongoDB 集合中物件陣列中過濾查詢的元素。為了僅檢索 MongoDB 中物件陣列中查詢的元素,讓我們首先建立一個包含文件物件陣列的集合。查詢如下:> db.objectArray.insert({"Persons":[ {"PersonName":"Adam", "PersonSalary":25000}, {"PersonName":"Larry", "PersonSalary":27000 }]}); WriteResult({ "nInserted" : 1 }) > db.objectArray.insert({"Persons":[ {"PersonName":"David", "PersonSalary":32000}, {"PersonName":"Carol", "PersonSalary":77000 }]}); WriteResult({ "nInserted" : 1 })現在您可以使用 find() 顯示所有文件。查詢如下:> db.objectArray.find().pretty();以下是輸出:{ "_id" : ObjectId("5c6bfadc68174aae23f5ef53"), "Persons" ... 閱讀更多

1K+ 次瀏覽
要從 MongoDB 中的集合中檢索文件,您需要使用 find() 方法。語法如下:db.yourCollectionName.find();以上語法將返回 MongoDB 中集合中的所有文件。為了理解以上語法,讓我們建立一個包含文件的集合。建立文件的查詢如下:> db.retrieveAllStudents.insertOne({"StudentId":"STUD101", "StudentName":"David", "StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c6bf5cf68174aae23f5ef4e") } > db.retrieveAllStudents.insertOne({"StudentId":"STUD102", "StudentName":"Carol", "StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c6bf5e968174aae23f5ef4f") } > db.retrieveAllStudents.insertOne({"StudentId":"STUD103", "StudentName":"Maxwell", "StudentAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c6bf5f768174aae23f5ef50") } > db.retrieveAllStudents.insertOne({"StudentId":"STUD104", "StudentName":"Bob", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ... 閱讀更多

578 次瀏覽
如果您想刪除集合中的所有文件,您可以使用 deleteMany()。讓我們首先建立一個集合並向其中插入一些文件:> db.deleteDocumentsDemo.insert({"Name":"Larry", "Age":23}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Mike", "Age":21}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Sam", "Age":24}); WriteResult({ "nInserted" : 1 })現在顯示集合中的所有文件。查詢如下:> db.deleteDocumentsDemo.find().pretty();以下是輸出:{ "_id" : ObjectId("5c6ab0e064f3d70fcc914805"), "Name" : "Larry", "Age" : 23 } { "_id" : ObjectId("5c6ab0ef64f3d70fcc914806"), "Name" : "Mike", "Age" : 21 } { "_id" : ObjectId("5c6ab0f864f3d70fcc914807"), "Name" : "Sam", "Age" : 24 } ... 閱讀更多

258 次瀏覽
要從 MongoDB 集合中刪除文件,您可以使用 deleteOne() 方法。讓我們首先建立一個集合並向其中插入一些文件:> db.deleteDocumentsDemo.insert({"Name":"Larry", "Age":23}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Mike", "Age":21}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Sam", "Age":24}); WriteResult({ "nInserted" : 1 })現在顯示集合中的所有文件。查詢如下:> db.deleteDocumentsDemo.find().pretty();以下是輸出:{ "_id" : ObjectId("5c6ab0e064f3d70fcc914805"), "Name" : "Larry", "Age" : 23 } { "_id" : ObjectId("5c6ab0ef64f3d70fcc914806"), "Name" : "Mike", "Age" : 21 } { "_id" : ObjectId("5c6ab0f864f3d70fcc914807"), "Name" : "Sam", ... 閱讀更多

354 次瀏覽
要從 MongoDB 集合中刪除文件,您需要使用 remove() 方法。語法如下:db.yourCollectionName.remove(yourDeleteValue);這裡,讓我們建立一個包含一些文件的集合。查詢如下:>db.deleteDocuments.insert({"UserId":1, "UserName":"Bob", "UserTechnicalSubject":"Introducti on to PL/SQL"}); WriteResult({ "nInserted" : 1 }) >db.deleteDocuments.insert({"UserId":2, "UserName":"Carol", "UserTechnicalSubject":"Introduction to MongoDB"}); WriteResult({ "nInserted" : 1 }) >db.deleteDocuments.insert({"UserId":3, "UserName":"John", "UserTechnicalSubject":"Introduction to MySQL"}); WriteResult({ "nInserted" : 1 }) >db.deleteDocuments.insert({"UserId":4, "UserName":"Maxwell", "UserTechnicalSubject":"Introduction to SQL Server"}); WriteResult({ "nInserted" : 1 })您可以使用 find() 命令顯示我們在上面建立的集合中的所有文件。查詢如下:> db.deleteDocuments.find().pretty();以下是 ... 閱讀更多

251 次瀏覽
要將新文件插入 MongoDB 集合中,您需要使用 insert() 方法或 save() 方法。情況 1:使用 insert() 方法。語法如下:db.yourCollectionName.insert(yourDocument);情況 2:使用 save() 方法。語法如下:db.yourCollectionName.save(yourDocument);在以上語法中,如果您的集合名稱不存在,則 MongoDB 將建立一個新集合並將文件插入該集合。以下是兩種情況下插入新文件的查詢。情況 1:使用 insert() 方法。查詢如下:> db.userInformation.insert({ ... "Name":"John", ... Age:30, ... isStudent:false, ... "Subjects":["Introduction to java", "Introduction to MongoDB"] ... ... 閱讀更多

626 次瀏覽
您可以使用 Python 中的 pymongo 庫連線到 MongoDB 資料庫,並使用它在 Python 中插入、更新、刪除等物件。該庫開箱即用地支援 Python 日期時間物件,您無需執行任何特殊操作即可使用 PyMongo 將日期插入 Mongo。例如,示例from pymongo import MongoClient # 這將嘗試連線到預設埠和主機上的 MongoDB client = MongoClient() db = client.test_database # 將給定的字典插入到 objects 集合中: result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Object inserted!")輸出這將給出以下輸出:物件已插入!注意:始終使用 ... 閱讀更多

1K+ 次瀏覽
您可以使用 Python 中的 pymongo 庫連線到 MongoDB 資料庫,並使用它在 Python 中插入、更新、刪除等物件。該庫開箱即用地支援 Python datetime 物件,您無需執行任何特殊操作即可使用 PyMongo 將日期插入 Mongo。示例from pymongo import MongoClient # 這將嘗試連線到預設埠和主機上的 MongoDB client = MongoClient() db = client.test_database # 將給定的字典插入到 objects 集合中: result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Object inserted!")輸出這將給出以下輸出 -Object inserted!注意 - 始終使用 datetime.datetime.utcnow(),它返回 ... 閱讀更多