I have an object stored in MongoDB which looks like:
{
_id: 123
name: "xyz"
attrib:
{
address: "123 xyz rd",
phone: "123-456-7890"
}
}
I want to flatted this structure, so that there is no attrib
field, I just have address
and phone
field along with name
and _id
.
So far, this is what I've tried:
db.emp.aggregate(
{
$project : {
{ addr : '$review.attrib.address' },
{ phn : '$review.votes.phone' },
}
}
);
Can anyone help me further?
I tried it:
db.abc.insert({
_id: 123,
name: "xyz",
attrib: {
address: "123 xyz rd",
phone: "123-456-7890"
}
});
db.abc.aggregate(
{
$project : {
_id:1,
name:1,
addr : '$attrib.address',
phn : '$attrib.phone'
}
}
);
More detail, you can see:use $project to rename fields
http://docs.mongodb.org/manual/reference/aggregation/project/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With