1

I am inserting json file into Mongodb(with Scala/Play framework) and the same getting/downloading it into my view page for some other requirement, but this time it is coming with one "_id" parameter in the json file.

But I need only my actual json file only that is not having any any "_id" parameter. I have read the Mongodb tutorial, that by default storing it with one _id for any collection document.

Please let me know that how can I get or is there any chance to get my actual json file without any _id in MongoDB.

this is the json result which is storing in database(I don't need that "_id" parameter)

{
        "testjson": [{
        "key01": "value1",
        "key02": "value02",
        "key03": "value03"
    }],
  "_id": 1
 }
Dhana
  • 711
  • 3
  • 14
  • 40

2 Answers2

2

You can use this query in the shell:

db.testtable.find({},{"_id" : false})

Here we are telling mongoDB not to return _id from the collection. You can also use 0 instead of false, like this:

db.testtable.find({},{"_id" : 0})

for scala you need to convert it in as per the driver syntax.

Himanshu Bhandari
  • 1,769
  • 2
  • 23
  • 37
2

If you have a look at ReactiveMongo dev guide and to its API, you can see it support projection in a similar way as the MongoDB shell.

Then you can understand that you can do

collection.find(selector = BSONDocument(), projection = BSONDocument("_id" -> 0))

Or, as you are using JSON serialization:

collection.find(selector = Json.obj(), projection = Json.obj("_id" -> 0))
cchantep
  • 9,118
  • 3
  • 30
  • 41
  • thanks you for your reply, I have added as per your comment, but this time also I can see _id is appended in my json. Edited the above query in description – Dhana Apr 13 '16 at 07:53
  • I have added the same thing at one more place in my other file, now it is working fine now. Thanks for your help !! – Dhana Apr 13 '16 at 08:58