push a new item to the array and determine whether can increase 1

71 views
Skip to first unread message

zhengchun

unread,
Mar 20, 2012, 9:39:59 AM3/20/12
to mongod...@googlegroups.com
the collection structure:

{
    _id : objectid()
    categories : [1,2]
    total_category : 2
    items : [
      {
          id
         category 
         .....
      }
    ]
}

now,i want push a new item object to the items fields,and push a category to the caregorys field and increase 1 for the total_category field when the new object 's category not exist in the categories array.
i noticed that use a update statement with the findAndModify not be finished,i must need two statement can do it. 

first : db.colls.update({},push item to the items.)
second : db.colls.findAndModify({category not exist},inc total_category to 1)

may i missing  something? can will use one statement do it? thanks any advice.

Sam Millman

unread,
Mar 20, 2012, 9:46:43 AM3/20/12
to mongod...@googlegroups.com
Is this being done from a server side language such as PHP or Java or something cos if so you can do a little hack here, here is an example in PHP:

$category = array('id' => 34543, 'name' => 'cheese');

update(array($category['name'] => array('$exists' => false)), array('$push' => array('items' => $category), '$inc' => array('total_category' => 1)))

Something like that would allow you to push a new category were it only exists since if you think about it you dont need to push a category on a document were it exists (or do you?) so you can do both at once providing the category does not already exist.

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/Gh2S2yXjxncJ.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Scott Hernandez

unread,
Mar 20, 2012, 9:47:38 AM3/20/12
to mongod...@googlegroups.com
Yes you can do it in one operation, see
http://www.mongodb.org/display/DOCS/Updating#Updating-%24inc

Something like: findAndModify({query:{...},
update:{$push:{<field>:<value>}, $inc: {<field>:1}}})

Sam Millman

unread,
Mar 20, 2012, 10:08:14 AM3/20/12
to mongod...@googlegroups.com
"Something like that would allow you to push a new category were it only exists since if you think about it you dont need to push a category on a document were it exists (or do you?) so you can do both at once providing the category does not already exist."

Just realised that was written incorrectly.

What I was meant to say is that the query I provided would allow you to $push a new category and $inc the total_category where that category does not already exist within the array. Since you are looking for uniqueness within the total_category field I am guessing you don't really wish to $push unless the category does not exist within the nested array (correct me if I am wrong) as such that should fulfill your needs.

zhengchun

unread,
Mar 20, 2012, 10:26:27 AM3/20/12
to mongod...@googlegroups.com
hi,samaaye,sorry about i not explain clear.

first,i always need  push a new item to the items fields(Regardless the category whether already or not exist  in the categories array)

next,i then determine that new item's category whether exist in the  categories  array,if not exist,will increase 1 to the total_category field.

在 2012年3月20日星期二UTC+8下午10时08分14秒,Sammaye写道:

> For more options, visit this group at
> http://groups.google.com/group/mongodb-user?hl=en.

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user+unsubscribe@googlegroups.com.

Sam Millman

unread,
Mar 20, 2012, 10:41:29 AM3/20/12
to mongod...@googlegroups.com
Ah in that case no. I don't think you can do this in one single query.

I think you'll need to do the $push first and then enforce the uniqueness $inc on the total_categories field with a findAndModify.

There might be some trick you can do to only $inc if another field is $push'ed but I don't think such a function exists natively in MongoDB.

To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/SAndPAHHgz0J.

To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages