Mongodb find_and_update $ne issue
I have a document:
active -> array of active id's
new_active -> array of active id's with some details
Now, I would like to add a new ID to active, but only if it's not present
in new_active. I dont want to change my schema, I would rather like to
understand why first version of the code works perfectly fine, while
second one does not.
In the first code I have used arrays for both active & new_active, and
this works fine: a =
MongoMapper.database.collection(:papers).find_and_modify(
:query => {
:active => {
'$nin' => [paper_id]
},
:new_active => {'$ne' => [paper_id]} #I know, $nin can also be
used
},
:update => {
'$push' =>{
:active => paper_id,
}
},
:upsert => false,
:new => true
This code works fine and pushes ID to the active if it is not present in
both arrays. Now, I wanted to have an array of hashes/ dicts in
new_active. This is when problem occurred. The reason for using $ne
instead of $nin is that $ne should return a result it the field doesn't
have particular value OR the field doesn't exist. In this case, paper_id
field in the new_active doesn't exist: a =
MongoMapper.database.collection(:papers).find_and_modify(
:query => {
:active => {
'$nin' => [paper_id]
},
:new_active => {
:paper_id => {'$ne' => [paper_id]}
}
},
:update => {
'$push' =>{
:active => paper_id,
}
},
:upsert => false,
:new => true
This code doesnt push paper_id into the active array. Why?
No comments:
Post a Comment