use custom primary key instead of "_id" ?

633 views
Skip to first unread message

Yang Y

unread,
May 4, 2011, 1:11:22 PM5/4/11
to mongodb-user
we want to use mongoimport to import into our mongodb,

the import process ***updates*** existing entries with the same key.

for example , in db, we have
{ name:"joe", salary:"100"}


and the new import data contains

{name:"joe", salary:"101"}

we want the old data to be update/overwritten, instead of adding a
second entry for {name:"joe"}


we could do this by setting {_id: } to be the same value as name:
field.


but the problem is that the input data we have come from an upstream
job, and it's bad to expose the internal logic of mongo to the output
of that stage; instead we know that the "name" will always be the
unique field.

so in this case, how can we tell mongo to use "name" as the unique
field, instead of "_id" ?? (I tried creating an unique index on
"name", but it does not allow overwrite, but just fails on save()
with the same key )


Thanks a lot
Yang

Gaetan Voyer-Perrault

unread,
May 4, 2011, 1:44:22 PM5/4/11
to mongod...@googlegroups.com
Take a look here:


mongoimport supports a --upsertFields arg
--upsertFields arg      comma-separated fields for the query part of the
                          upsert. You should make sure this is indexed.
I believe this is what you are looking for.

- Gates 


--
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.


Yang

unread,
May 4, 2011, 1:56:58 PM5/4/11
to mongod...@googlegroups.com
exactly, thanks!
Reply all
Reply to author
Forward
0 new messages