Insert only if not exists, if exists do nothing

11,335 views
Skip to first unread message

rksprst

unread,
Jan 18, 2011, 5:25:28 PM1/18/11
to mongodb-user
Hi,

I want to insert a value only if it doesn't already exists. If it
already exists, I don't want to do anything.

I want to avoid checking if the value is there as this insert will
happen many many times a second. "upsert" isn't what I want as I don't
want to update if it exists. Is there something else that can
accomplish this task?

Thanks,
Alex

Eliot Horowitz

unread,
Jan 18, 2011, 6:11:27 PM1/18/11
to mongod...@googlegroups.com
If you create a unique index on whatever your unique constraint is,
then that's what will happen.

> --
> 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...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

rksprst

unread,
Jan 18, 2011, 6:18:09 PM1/18/11
to mongodb-user
So after I create the unique insert, do I just do a normal insert?

Eliot Horowitz

unread,
Jan 18, 2011, 6:19:26 PM1/18/11
to mongod...@googlegroups.com
yes

rksprst

unread,
Jan 18, 2011, 7:23:22 PM1/18/11
to mongodb-user
It seems that I can only insert a whole document. I just want to
update a value in the document. Is there a way to do it with the
Update.Set or similar syntax? I also need to use a query so that it
will find do the set only on the documents that fit a specified
query.

e.g. I want to do the following:

QueryComplete query = Query.And(
Query.EQ("MemGuid", memGuid),
Query.EQ("Sessions.SessionId", sessionId)
);
var update = Update.PushAll("Sessions.$.Events",
events.ToArray()).Set("Last ", utcNow);

database.GetCollection<TrackedUser>("TrackedItems").Update(query,
update, UpdateFlags.Upsert, SafeMode.True);

But where the .Set("Last ", utcNow) only sets the value if it is null
(i.e. the value doesn't exist).

I'm using the 10gen c# driver. Also, per my other question on here
( http://groups.google.com/group/mongodb-user/browse_thread/thread/1746866abb30abca#
) I want to batch the requests.

Robert Stam

unread,
Jan 18, 2011, 7:31:31 PM1/18/11
to mongodb-user
You'll probably have to break it up into two operations so you can
test that Last is not null in one of them. I'm not aware of any
conditional $set update modifiers.

Also, a good way to experiment (and to ask the questions) is to use
the Mongo shell. Once you get it working there you can figure out how
to express the same update in C#. If you use temporary variables for
your query and update arguments (as you did) you can use the ToJson()
method to see if they are equivalent to the query and update that
worked in the shell.

On Jan 18, 7:23 pm, rksprst <alex.kamin...@gmail.com> wrote:
> It seems that I can only insert a whole document. I just want to
> update a value in the document. Is there a way to do it with the
> Update.Set or similar syntax? I also need to use a query so that it
> will find do the set only on the documents that fit a specified
> query.
>
> e.g. I want to do the following:
>
>             QueryComplete query = Query.And(
>                     Query.EQ("MemGuid", memGuid),
>                     Query.EQ("Sessions.SessionId", sessionId)
>                 );
>             var update = Update.PushAll("Sessions.$.Events",
> events.ToArray()).Set("Last ", utcNow);
>
> database.GetCollection<TrackedUser>("TrackedItems").Update(query,
> update, UpdateFlags.Upsert, SafeMode.True);
>
> But where the .Set("Last ", utcNow) only sets the value if it is null
> (i.e. the value doesn't exist).
>
> I'm using the 10gen c# driver. Also, per my other question on here
> (http://groups.google.com/group/mongodb-user/browse_thread/thread/1746...

rksprst

unread,
Jan 18, 2011, 7:37:37 PM1/18/11
to mongodb-user
I'm going to try doing it with Query.EQ("Last", null). Would that
work?

Also, I have around 10 fields where I want to update them if they are
not null. It seems like I would have to do 10 queries to figure out
which one is null or not. Is that correct?

Robert Stam

unread,
Jan 18, 2011, 7:39:49 PM1/18/11
to mongodb-user
You have to use Query.EQ("Last", BsonNull.Value).

If someone figures out how to do what you want to do in the shell we
can translate that to C#, otherwise I think it is one update for every
field that you want to check for null.

kishore kumar

unread,
Nov 15, 2013, 9:39:42 AM11/15/13
to mongod...@googlegroups.com
How do we do it in a shard environment.
Reply all
Reply to author
Forward
0 new messages