Reputation: 1381
I am trying to get the _id
from a collection without getting the ObjectId part.
When I try and query it this way:
db.collection.aggregate([
{'$unwind': '$_id'},
{
'$project': {
'_id': '$_id'
}
}
])
This returns:
ObjectId("51234yhf789")
ObjectId("51234dff779")
ObjectId("51234yhf745")
ObjectId("51234dff123")
ObjectId("51234d45123")
Even when I try: '_id': '$_id'.valueOf()
, I get the same thing.
How do I get:
51234yhf789
51234dff779
51234yhf745
51234dff123
51234d45123
Need help for MongoDB V3.6 and below
Upvotes: 4
Views: 2017
Reputation: 17935
On MongoDB version > 4.0
you can use $toString to convert ObjectId()
to string :
db.collection.aggregate([
{'$unwind': '$_id'},
{
'$project': {
'_id': {$toString : '$_id' }
}
}
])
Upvotes: 4