Mongodb 如何聚合以将_id作为对象属性
我有以下收藏:Mongodb 如何聚合以将_id作为对象属性,mongodb,mongodb-query,Mongodb,Mongodb Query,我有以下收藏: { _id: 1, type: "Feature", properties: { name: "Name 1", <other properties here> } }, { _id: 2, type: "Feature", properties: { name: "Name 2", <other properties here> } } { _id:1, 键入:“功能”, 特性:{ 名称:
{
_id: 1,
type: "Feature",
properties: {
name: "Name 1",
<other properties here>
}
},
{
_id: 2,
type: "Feature",
properties: {
name: "Name 2",
<other properties here>
}
}
{
_id:1,
键入:“功能”,
特性:{
名称:“名称1”,
}
},
{
_id:2,
键入:“功能”,
特性:{
姓名:“姓名2”,
}
}
如何编写MongoDB聚合查询以返回以下内容
{
data: {
type: "Feature",
properties: {
_id: 1,
name: "Name 1",
<other properties here>
}
}
},
{
data: {
type: "Feature",
properties: {
_id: 2,
name: "Name 2",
<other properties here>
}
}
}
{
数据:{
键入:“功能”,
特性:{
_id:1,
名称:“名称1”,
}
}
},
{
数据:{
键入:“功能”,
特性:{
_id:2,
姓名:“姓名2”,
}
}
}
因此,\u id
应该成为属性的一个属性
,每个文档都应该作为数据的对象返回
1-如果只需要_id位于属性内部,而不需要位于数据对象中
db.collection.aggregate([
{
$group: {
_id: "$_id",
type: {
$first: "$type"
},
properties: {
$first: {
_id: "$_id",
name: "$properties.name"
}
}
}
},
{
$project: {
_id: 0,
data: "$$ROOT"
}
},
{
$project: {
"data._id": 0
}
}
])
检查这个
2-如果只需要在properties对象内部获取_id,而不关心它是否在数据中,那么
你可以用这个
db.collection.aggregate([
{
$group: {
_id: "$_id",
type: {
$first: "$type"
},
properties: {
$first: {
_id: "$_id",
name: "$properties.name"
}
}
}
},
{
$project: {
_id: 0,
data: "$$ROOT"
}
}
])
检查此项尝试此项
1-如果只需要_id位于属性内部,而不需要位于数据对象中
db.collection.aggregate([
{
$group: {
_id: "$_id",
type: {
$first: "$type"
},
properties: {
$first: {
_id: "$_id",
name: "$properties.name"
}
}
}
},
{
$project: {
_id: 0,
data: "$$ROOT"
}
},
{
$project: {
"data._id": 0
}
}
])
检查这个
2-如果只需要在properties对象内部获取_id,而不关心它是否在数据中,那么
你可以用这个
db.collection.aggregate([
{
$group: {
_id: "$_id",
type: {
$first: "$type"
},
properties: {
$first: {
_id: "$_id",
name: "$properties.name"
}
}
}
},
{
$project: {
_id: 0,
data: "$$ROOT"
}
}
])
选中此项简单的结构操作即可:
db.collection.aggregate([
{
$project: {
data: {
type: "$type",
properties: {
$mergeObjects: [
{_id: "$_id"},
"$properties"
]
}
}
}
}
])
简单的结构操纵就足够了:
db.collection.aggregate([
{
$project: {
data: {
type: "$type",
properties: {
$mergeObjects: [
{_id: "$_id"},
"$properties"
]
}
}
}
}
])
我无法使用您的数据集,因为它不是RFC 8259格式的。
但是,假设您的数据集格式正确,如下所示。如果字段匹配,发出的命令将接近关闭
{
"data":{
"Feature":{
"properties":{
"_id1":[
"object_of_data1",
"object_of_data2",
"object_of_data3"
],
"_id2":[
"object_of_data1",
"object_of_data2",
"object_of_data3"
],
"_id3":[
"object_of_data1",
"object_of_data2",
"object_of_data3"
]
}
}
}
}
然后我将使用以下命令:
db.orders.aggregate([
{ $match: { properties: "_id1" } },
{ $group: { _id1: "object_of_data1", "object_of_data2"
"object_of_data3" } } }
])
如需进一步阅读,请参阅:我无法利用您的数据集,因为它不是RFC 8259格式的。
但是,假设您的数据集格式正确,如下所示。如果字段匹配,发出的命令将接近关闭
{
"data":{
"Feature":{
"properties":{
"_id1":[
"object_of_data1",
"object_of_data2",
"object_of_data3"
],
"_id2":[
"object_of_data1",
"object_of_data2",
"object_of_data3"
],
"_id3":[
"object_of_data1",
"object_of_data2",
"object_of_data3"
]
}
}
}
}
然后我将使用以下命令:
db.orders.aggregate([
{ $match: { properties: "_id1" } },
{ $group: { _id1: "object_of_data1", "object_of_data2"
"object_of_data3" } } }
])
如需进一步阅读,请参阅: