Update README.md

pull/2/head
Louis Chatriot 12 years ago
parent ceb4b4d15f
commit c739ec696b
  1. 15
      README.md

@ -119,11 +119,19 @@ db.update({ system: 'solar' }, { $set: { system: 'solar system' } }, { multi: tr
// Field 'system' on Mars, Earth, Jupiter now has value 'solar system' // Field 'system' on Mars, Earth, Jupiter now has value 'solar system'
}); });
// Incrementing the value of a non-existing field in a subdocument // Incrementing the value of a non-existing field in a subdocument by using the dot-notation
db.update({ planet: 'Mars' }, { $inc: { "data.satellites": 2 } }, {}, function () { db.update({ planet: 'Mars' }, { $set: { "data.satellites": 2, "data.red": true } }, {}, function () {
// Mars document now is { _id: 'id1', system: 'solar', inhabited: false // Mars document now is { _id: 'id1', system: 'solar', inhabited: false
// , data: { satellites: 2 } // , data: { satellites: 2, red: true }
// } // }
// Not that to set fields in subdocuments, you HAVE to use dot-notation
// Using object-notation will just replace the top-level field
db.update({ planet: 'Mars' }, { $set: { date: { satellites: 3 } } }, {}, function () {
// Mars document now is { _id: 'id1', system: 'solar', inhabited: false
// , data: { satellites: 3 }
// }
// You lost the "data.red" field which is probably not the intended behavior
});
}); });
// Upserting a document // Upserting a document
@ -133,6 +141,7 @@ db.update({ planet: 'Pluton' }, { planet: 'Pluton', inhabited: false }, { upsert
}); });
// If you upsert with a modifier, the upserted doc is the query modified by the modifier // If you upsert with a modifier, the upserted doc is the query modified by the modifier
// This is simpler than it sounds :)
db.update({ planet: 'Pluton' }, { $set: { inhabited: false } }, { upsert: true }, function () { db.update({ planet: 'Pluton' }, { $set: { inhabited: false } }, { upsert: true }, function () {
// A new document { _id: 'id5', planet: 'Pluton', inhabited: false } has been added to the collection // A new document { _id: 'id5', planet: 'Pluton', inhabited: false } has been added to the collection
}); });

Loading…
Cancel
Save