Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -
> db.demo601.insertOne( ... { ... id:1, ... userDetails: ... { ... userName:"John", ... userMailId:"John@gmail.com" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e95ff5ced011c280a0905c7") } > > db.demo601.insertOne( { id:2, userDetails: { userName:"Carol", userMailId:"Carol@gmail.com" } } );{ "acknowledged" : true, "insertedId" : ObjectId("5e95ff71ed011c280a0905c8") }
Hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find () -
> db.demo601.find();
Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5e95ff5ced011c280a0905c7"), "id" : 1, "userDetails" : { "userName" : "John", "userMailId" : "John@gmail.com" } } { "_id" : ObjectId("5e95ff71ed011c280a0905c8"), "id" : 2, "userDetails" : { "userName" : "Carol", "userMailId" : "Carol@gmail.com" } }
Sau đây là truy vấn để cập nhật -
>db.demo601.update({_id:ObjectId("5e95ff71ed011c280a0905c8")},{$set:{userMailId:"Carol@yahoo.com"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find () -
> db.demo601.find();
Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5e95ff5ced011c280a0905c7"), "id" : 1, "userDetails" : { "userName" : "John", "userMailId" : "John@gmail.com" } } { "_id" : ObjectId("5e95ff71ed011c280a0905c8"), "id" : 2, "userDetails" : { "userName" : "Carol", "userMailId" : "Carol@gmail.com" }, "userMailId" : "Carol@yahoo.com" }