Database Reference
In-Depth Information
The insert command automatically creates the _ id key if it is not
specified.Moststreamingapplicationswillwanttoexplicitlyspecifythe_ id
to make updates easier:
> db.first.insert({_id:"test:201312010000"
,ts:new Date(2013,11,01,00,00),metric:"test",n:1});
> db.first.find();
{ "_id" : "test:201312010000",
"ts" : ISODate("2013-12-01T08:00:00Z"), "metric" :
"test", "n" : 1 }
Attempting to insert a document with a duplicate key fails with an error:
> db.first.insert({_id:"test:201312010000",
ts:new Date(2013,11,01,00,00),metric:"test",n:1});
E11000 duplicate key error index: mine.first.$_id_
dup key: { : "test:201312010000" }
Updating documents is accomplished via the update command. This
command takes a query, which will often simply be a match on the _ id
field, and a replacement document. By default, this completely replaces the
matching document(s), requiring that the entire document be replicated.
For example, this is probably not the desired result for this update:
> db.first.update({_id:"test:201312010000"},{n:2});
> db.first.find()
{ "_id" : "test:201312010000", "n" : 2 }
Notice that the entire document has been replaced, when the desired result
was probably to simply update the n field. To do that the special $set field
is used to define the fields to update:
bb.first.drop();
> db.first.insert({_id:"test:201312010000",
ts:new Date(2013,11,01,00,00),metric:"test",n:1});
>
db.first.update({_id:"test:201312010000"},{"$set":{n:2}});
> db.first.find()
{ "_id" : "test:201312010000",
Search WWH ::




Custom Search