Questions : remove _id from mongodb result java
2022-09-28T17:02:29+00:00 2022-09-28T17:02:29+00:00
831
My code is
DBCollection collection = db.getCollection["volume"];
DBCursor cursor = collection.find[];
DBObject resultElement = cursor.next[];
Map resultElementMap = resultElement.toMap[];
System.out.println[resultElementMap];
And the result is:
{_id=521b509d20954a0aff8d9b02, title={ anycodings_java "text" : "Volume Of Work Orders" , "x" : anycodings_java -20.0}, xAxis={ "title" : { "text" : "2012 anycodings_java "} , "categories" : [ "Jan" , "Feb" , anycodings_java "Mar" , "Apr" , "May" , "Jun" , "Jul" , anycodings_java "Aug" , "Sep" , "Oct" , "Nov" , "Dec"]}, anycodings_java yAxis={ "min" : 1000.0 , "max" : 7000.0 , anycodings_java "title" : { "text" : "Volume[K]"} , anycodings_java "plotLines" : [ { "label" : { "text" : anycodings_java "Average" , "x" : 25.0} , "color" : "black" anycodings_java , "width" : 2.0 , "value" : 30.0 , anycodings_java "dashStyle" : "solid"}]}, legend={ anycodings_java "backgroundColor" : "#FFFFFF" , "reversed" : anycodings_java true}, series=[ { "name" : "Volume" , anycodings_java "showInLegend" : false , "data" : [ 2909.0 , anycodings_java 3080.0 , 4851.0 , 3087.0 , 2960.0 , 2911.0 anycodings_java , 1900.0 , 3066.0 , 3029.0 , 5207.0 , 3056.0 anycodings_java , 3057.0]}]}
I need to remove _id from the result. I anycodings_java understand i need to play around with anycodings_java collection.find[], but please can anyone anycodings_java help me? Am not able to get sesired result
Total Answers 2
29
Answers 1 : of remove _id from mongodb result java
Two options:
You can remove the "_id" field from the anycodings_mongodb map created:
...
resultElementMap.remove["_id"];
System.out.println[resultElementMap];
Or you can ask the query results to not anycodings_mongodb include the _id field:
DBObject allQuery = new BasicDBObject[];
DBObject removeIdProjection = new basicDBObject["_id", 0];
DBCollection collection = db.getCollection["volume"];
DBCursor cursor = collection.find[allQuery, removeIdProjection];
DBObject resultElement = cursor.next[];
Map resultElementMap = resultElement.toMap[];
System.out.println[resultElementMap];
See the documentation on projections for anycodings_mongodb all of the details.
0
2022-09-28T17:02:29+00:00 2022-09-28T17:02:29+00:00Answer Link
mRahman
5
Answers 2 : of remove _id from mongodb result java
Another option to consider, if you are anycodings_mongodb reading the results iteratively, is anycodings_mongodb doing something like this:
final FindIterable foundResults = collection.find[];
for [final Document doc : foundResults] {
doc.remove["_id"];
// doc.toJson[] no longer has _id
}
0
2022-09-28T17:02:29+00:00 2022-09-28T17:02:29+00:00Answer Link
joy
I’m pretty new with mongo and nodejs
I’ve a json as result of my query and I simply want to return the result as an http request, as following:
app.get['/itesms', function[req, res] {
items.find[].toArray[function [err, array] {
res.send[array];
}]
}];
It works, only problem is that I want to hide the _id fields [recursively] from the result.
Any suggestion to do that in an elegant way?
Try this solution:
app.get['/itesms', function[req, res] {
items.find[{}, { _id: 0 }].toArray[function [err, array] {
res.send[array];
}]
}];
3
The usual .find[{}, {_id:0}]
approach wasn’t working for me, so I went hunting and found in another SO answer that in version 3 of the Mongo API, you need to write it like this: .find[{}, {projection:{_id:0}}]
. So, for example:
let docs = await db.collection["mycol"].find[{}, {projection:{_id:0}}].toArray[];
It seems that [in the nodejs API, at least] you can also write it like this:
let docs = await db.collection["mycol"].find[{}].project[{_id:0}].toArray[];
2
The problem is that you can’t project inclusions and exclusions, ie you can’t run a query with a ‘project’ statement that declares what should be included in the response as well as what must be excluded from the response.
From MongoDB documentation:
A projection cannot contain both include and exclude specifications, except for the exclusion of the _id field. In projections that explicitly include fields, the _id field is the only field that you can explicitly exclude.
The way I handled this problem was to go to the end of the process, right before returning the response:
const dbObjectJSON = dbObject.toJson[];
delete dbObjectJSON._id;
delete dbObjectJSON.__v;
...
response.json[dbObjectJSON];
Hope this helps.