0

My project was build on GMONGO framework and all the domains were created with attribute id as a string.So grail frame work will create mongodb collections with _id attribute when application is app. In UI there was a limitation to retrive _id.So in service "_id" is rewrite to "id" to overcome the issue. Could we created collections with "id" by default rather than "_id"?

Raj
  • 173
  • 3
  • 9

1 Answers1

2

No you cant. Mongo will automaticaly create _id if you dont specify any.

https://docs.mongodb.com/v3.2/reference/glossary/

In your case you can add index to id field, and when run queries just add projection to exclude _id.

So you will have something like this in data

{_id: mongoDbID, id: yourId, ...}

And run query like this

collection.find({id: yourId}).project({_id: 0}).toArray();

You will get this as an result

{id: yourId, ...}

Hope this helps.

Mykola Borysyuk
  • 3,373
  • 1
  • 18
  • 24
  • _id will be create if you are not define id in domain,but i am defining id attribute in my domain even though mongo is creating _id rather than id. My requirement is get a gson objecct with key is id not _id – Raj Jan 29 '17 at 06:05
  • Is there any way to create id rather than _id? – Raj Jan 30 '17 at 08:51
  • I answer that you cant. This is how mongo works. You can choose another DB, or just dont care about _id in system. Hope this helps. – Mykola Borysyuk Jan 30 '17 at 13:49