Weitere ähnliche Inhalte
Ähnlich wie Mongo db modifiers (20)
Kürzlich hochgeladen (20)
Mongo db modifiers
- 2. $inc> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"uName" : "anish",
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"age" : 32
}
Increment the uName=anish age to 1
> db.user.update({"_id" :
ObjectId("513c6ef1cfce9090d3fd8b1d")},{"$inc" : {"age" :
1}})
> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"uName" : "anish",
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"age" : 33
}
- 3. $set
> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"uName" : "anish",
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"age" : 33
}
Set the uName=anish age to 31
> db.user.update({"_id" :
ObjectId("513c6ef1cfce9090d3fd8b1d")},{"$set" : {"age" : 31}})
> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"uName" : "anish",
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"age" : 31,
}
- 4. $unset
> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"uName" : "anish",
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"age" : 33
}
Remove the age key
> db.user.update({"_id" :
ObjectId("513c6ef1cfce9090d3fd8b1d")},{"$unset" : {"age" : 31}})
> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"uName" : "anish"
}
- 5. $PUSHAdd a array to the exisitng document
> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"uName" : "anish",
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"age" : 33
}
PUSH the remark array in the document
db.user.update({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")}, {$push : {"remarks" : {"name":"anish","rate":"good"}}})
> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"remarks" : [
{
"name" : "anish",
"rate" : "good"
}
],
"uName" : "anish"
}
Add existing to the remarks array
> db.user.update({"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d")}, {$push : {"remarks" : {"name":"nath","rate":"bad"
> db.user.findOne()
{
"_id" : ObjectId("513c6ef1cfce9090d3fd8b1d"),
"date" : ISODate("2013-03-10T11:30:50.555Z"),
"remarks" : [
{
"name" : "anish",
"rate" : "good"
},
{
"name" : "nath",
"rate" : "bad"
}
],
"uName" : "anish"
}