2

I came across this -

MongoDB atomic "findOrCreate": findOne, insert if nonexistent, but do not update

What I want is -

  • If document is Absent, I insert one and return SUCCESS
  • If document is Present, return FAILURE

Can I achieve this?

Community
  • 1
  • 1
abipc
  • 997
  • 2
  • 13
  • 35
  • 3
    If you insert on a unique key such as _id then it will have this behaviour by default – Sammaye Oct 22 '13 at 08:24
  • Yea..I am doing something similar.. I am doing insert on a unique field to achieve this.. – abipc Oct 22 '13 at 10:36
  • as @Sammaye stated above, [`insert()`](http://docs.mongodb.org/manual/reference/method/db.collection.insert/#db.collection.insert) does *exactly* that. Have you had any problems with `insert()`? – Cristian Lupascu Oct 22 '13 at 11:40
  • Sammaye solved my problem.. I am using what he suggested but I had figured the same out myself as well.. – abipc Oct 22 '13 at 14:11

0 Answers0