MongoDB Insert, find, update and delete
abc123> db.eswar.insertOne({SRN:001,SName:'a',yr:3})
{
acknowledged: true,
insertedId: ObjectId("66f392b1d8b1cbf0117c527c")
}
abc123> db.eswar.insertOne({SRN:002,SName:'b',yr:3})
{
acknowledged: true,
insertedId: ObjectId("66f392cbd8b1cbf0117c527d")
}
abc123> db.eswar.insertOne({SRN:003,SName:'c',yr:2})
{
acknowledged: true,
insertedId: ObjectId("66f392e3d8b1cbf0117c527e")
}
abc123> db.eswar.find()
[
{
_id: ObjectId("66f392b1d8b1cbf0117c527c"),
SRN: 1,
SName: 'a',
yr: 3
},
{
_id: ObjectId("66f392cbd8b1cbf0117c527d"),
SRN: 2,
SName: 'b',
yr: 3
},
{
_id: ObjectId("66f392e3d8b1cbf0117c527e"),
SRN: 3,
SName: 'c',
yr: 2
}
]
abc123> show collections
eswar
abc123> db.eswar.update({SName:'C'},{$set:{yr:3}})
DeprecationWarning: Collection.update() is deprecated. Use updateOne, updateMany, or bulkWrite.
{
acknowledged: true,
insertedId: null,
matchedCount: 0,
modifiedCount: 0,
upsertedCount: 0
}
abc123> db.eswar.update({SName:'c'},{$set:{yr:3}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
abc123> db.eswar.find()
[
{
_id: ObjectId("66f392b1d8b1cbf0117c527c"),
SRN: 1,
SName: 'a',
yr: 3
},
{
_id: ObjectId("66f392cbd8b1cbf0117c527d"),
SRN: 2,
SName: 'b',
yr: 3
},
{
_id: ObjectId("66f392e3d8b1cbf0117c527e"),
SRN: 3,
SName: 'c',
yr: 3
}
]
abc123> db.eswar.delete({SName:'c'})
TypeError: db.eswar.delete is not a function
abc123> db.eswar.deleteOne({SName:'c'})
{ acknowledged: true, deletedCount: 1 }
abc123> db.eswar.find()
[
{
_id: ObjectId("66f392b1d8b1cbf0117c527c"),
SRN: 1,
SName: 'a',
yr: 3
},
{
_id: ObjectId("66f392cbd8b1cbf0117c527d"),
SRN: 2,
SName: 'b',
yr: 3
}
]
abc123>
Comments
Post a Comment