mongodb - Update an Element if Position is Unknown with Upsert -
It seems that you (/ I) can not be both one and the other.
If you do (python):
findDct = {"_id": ObjectId ("535e3ab9c36b4417d031402f"), 'events.ids': '176976332'} print Col.update (findDct, {"$ set": {"Events. $ Foo": "bar"}}, upstart = true)
It will be thrown:
pymongo.errors.DuplicateKeyError: insertDocument :: 11000 E11000 Duplicate Key Error Index: test.col. $ _ Id_ dup: ::: ObjectId ('535e3ab9c36b4417d031402f')}
This is because the "_id" course tries to put an index and Mongo into a new document Because the search query fails on its 'events.ids':' 176976332 ''
part ().
Is it possible to update an unknown element in Aray with upstart?
Yes it is, but you're going to wrongly discuss this element instead of "searching Instead of doing "you are not sure whether it exists or not, try applying the operator instead:
db.collection {" _id ": ObjectId (" 535e3ab9c36b4417d031402f "}, {" $ addToSet "}: {" event ": {" foo ":" bar "}}}, {" upset ": true}}
< P> Please note also from operator documentation that you do not want to use the $
operator with "upserts" Hia as a result of the name contains the value as "will be interpreted as a literal" (which includes "events. $ Foo"
) and should be put to actual fields in the document. Try to make sure that your array "Insert / Upstate" operation specifies the entire array content for this task.
With another optimization method, pymongo is already a good API for the driver, but this is a normal form:
{"events": {{"535e3ab9c36b4417d031402f"}}, "u" Pre>But still be very careful that you are not duplicating your sub-documentary array if you can clearly see that case but this is a method. Because each update will cascade down, even though first made the best case examples failed to add anything. But I hope you see the point.
Comments
Post a Comment