1

My fetched DBObject looks like following

{ "_id" : { "$oid" : "50c28ac1de86acf0bdfbeca0"} , 
"schedule" : { "startAt" : 1354926785198 , "endAt" : 1391155200000 , "repeatForever" : true , "interval" : 3600} , "destination" : "Storage-East"}

I want to extract JSON string sans "_id" so that I can deserialize it back to my Java object. If I use following I can remove the '_id' field and it is fine to get the Java object back from JSON String. Is there any other elegant way of doing this ?

dbObj.removeField("_id");
String jsonString = dbObj.toString();

// Now readValue from the json string 

thanks.

1 Answer 1

1

Instead of removing the data afterwords, just use results projections. You simply remove the _id by using the result projections in a find statement:

//find all documents where destination equals Storage-East, but exclude _id from result set
db.inventory.find( { "destination": 'Storage-East' }, { _id:0 } )

You can find the documentation http://docs.mongodb.org/manual/core/read-operations/#result-projections.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thanks for the doc pointer (newbie miss on the docs) :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.