jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
286 stars 65 forks source link

How do I create a unique index? #133

Open bidwbb opened 6 years ago

bidwbb commented 6 years ago

How do I create a unique index? Is it possible with mongolite?

psramkumar commented 6 years ago

related another Question to @mongolite Team how do we specify our own Unique key value

gladkia commented 5 years ago

Hi @psramkumar,

I've added support for unique indices here. This feature should be available in mongolite itself pretty soon, when the PR (link) is approved and merged.

popiula commented 5 years ago

Is it available yet? how can we create an unique index?

gladkia commented 5 years ago
  1. @Popiula, the PR has not been merged so far. I am still waiting for the response from @jeroen.

  2. Please have a look into the R tests for examples how to create a unique index.

martinschmelzer commented 5 years ago

Will there be progress in the future concerning unique indices?

seventm commented 4 years ago

There is a simple solution... use run command: mongo_connection$run('{"createIndexes": "collection_name", "indexes":[{"key":{"field_name":1}, "name": "my_new_index", "unique": true]}')

https://docs.mongodb.com/manual/reference/command/createIndexes/#dbcmd.createIndexes

odeda commented 4 years ago

There is a simple solution... use run command: mongo_connection$run('{"createIndexes": "collection_name", "indexes":[{"key":{"field_name":1}, "name": "my_new_index", "unique": true]}')

https://docs.mongodb.com/manual/reference/command/createIndexes/#dbcmd.createIndexes

Pretty old, but nevertheless I'm getting an error with this approach and am facing the same issue of trying to create a unique compound index using mongolite. Following mongodb docs: https://docs.mongodb.com/manual/core/index-unique/

m <- mongolite::mongo(mongo_url, collection = 'col', db = 'test')
m$run('{"db.col.createIndex( { "field_A": 1, "field_B": 1 }, { unique: true } )": 1}') 

Error: Invalid JSON object: {"db.col.createIndex( { "field_A": 1, "field_B": 1 }, { unique: true } )": 1}

BTW, if there's a simpler way to create unique indexes (compound or otherwise) would be happy to know Thanks!

seventm commented 4 years ago

m <- mongolite::mongo(mongo_url, collection = 'col', db = 'test') mongo_connection$run('{"createIndexes": "col", "indexes":[{"key":{"field_A":1, "field_B": 1}, "name": "field_A_B_index", "unique": true]}')

odeda commented 4 years ago

m <- mongolite::mongo(mongo_url, collection = 'col', db = 'test') mongo_connection$run('{"createIndexes": "col", "indexes":[{"key":{"field_A":1, "field_B": 1}, "name": "field_A_B_index", "unique": true]}')

Thank you, that did It