我尝试提出一个请求,以在一个请求中检索所有嵌套的子文档。但是我实际上有两个问题。
第一个是当我加入celestial_objects_id的子文档时(请参阅下面的代码以获取描述)。我有一个对象,但是期望该对象的数组。
第二个(可能与第一个关联)我尝试将字段“ celestial_objects.affiliations_id”与集合“ affiliations”结合在一起。但这会使字段'celestial_object'为空。
此请求在mongo shell中已经可以正常使用,但在mgo中无法正常工作。
db.systems
.aggregate([
{
$lookup: {
from: "celestial_objects",
localField: "celestial_objects_id",
foreignField: "_id",
as: "celestial_objects"
}
},
{
$lookup: {
from: "affiliations",
localField: "affiliations_id",
foreignField: "_id",
as: "affiliations"
}
},
{ "$unwind": "$celestial_objects"},
{
$lookup: {
from: "affiliations",
localField: "celestial_objects.affiliations_id",
foreignField: "_id",
as: "celestial_objects.affiliations"
}
},
])
我尝试用golang编写它,但是只有当我将其用作模型[]bson.M
或[]interface{}
时,此请求才能正常工作
但是,如果我使用结构,则数组'system.celestial_objects'变为null。我尝试了很多次来重新编写我的结构并复制粘贴标签,但是我没有任何尝试。
这是结构定义:
type Affiliation struct {
Id bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
Slug string `bson:"slug"`
}
type CelestialObject struct {
Id bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
Slug string `bson:"slug"`
Designation string `bson:"designation"`
ObjectType string `bson:"objectType"`
Coordinates Coordinates `bson:"coordinates"`
AffiliationsId []bson.ObjectId `bson:"affiliations_id"`
Affiliations []Affiliation `bson:"affiliations"`
Lore ObjectsLore `bson:"lore"`
Size int `bson:"size"`
}
type System struct {
Id bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
Slug string `bson:"slug"`
Coordinates Coordinates `bson:"coordinates"`
AffiliationsId []bson.ObjectId `bson:"affiliations_id"`
Affiliations []Affiliation `bson:"affiliations"`
CelestialObjectsId []bson.ObjectId `bson:"celestial_objects_id"`
CelestialObjects []CelestialObject `bson:"celestial_objects"`
}
这是发出请求的函数:
func (db *DbSystem) FindAllTest() ([]System, error) {
var ret []System
query := []bson.M{
{
"$lookup": bson.M{
"from": "celestial_objects",
"localField": "celestial_objects_id",
"foreignField": "_id",
"as": "celestial_objects",
},
},
{
"$lookup": bson.M{
"from": "affiliations",
"localField": "affiliations_id",
"foreignField": "_id",
"as": "affiliations",
},
},
{
"$unwind": bson.M{
"path": "$celestial_objects",
"preserveNullAndEmptyArrays": true,
},
},
{
"$lookup": bson.M{
"from": "affiliations",
"localField": "celestial_objects.affiliations_id",
"foreignField": "_id",
"as": "celestial_objects.affiliations",
},
},
}
err := db.collection.Pipe(query).All(&ret)
if err != nil {
return nil, err
}
return ret, nil
}
具有[] bson.M和[] interface {}的输出:
[
{
"_id": "5cd84eaea54d75d81a94ba0a",
"affiliations": [
{
"_id": "5cd84a1fa54d75d1bb45c751",
"name": "JAB",
"slug": "jab"
}
],
"affiliations_id": [
"5cd84a1fa54d75d1bb45c751"
],
"celestial_objects": { // <------- This should be in an array
"_id": "5cd9d1fd2fa5dbd585cbdbd9",
"affiliations": [
{
"_id": "5cd84a1fa54d75d1bb45c751",
"name": "JAB",
"slug": "jab"
}
],
"affiliations_id": [
"5cd84a1fa54d75d1bb45c751"
],
"coordinates": { ... },
"designation": "Earth planet",
"jump_points_id": [],
"lore": { ... },
"name": "Earth",
"objectType": "PLANET",
"size": 0,
"slug": "earth"
},
"celestial_objects_id": [
"5cd9d1fd2fa5dbd585cbdbd9"
],
"coordinates": { ... // content OK },
"name": "Solar system",
"slug": "solar-system"
}
]
使用[] System输出:
[
{
"_id": "5cd84eaea54d75d81a94ba0a",
"affiliations": [
{
"_id": "5cd84a1fa54d75d1bb45c751",
"name": "JAB",
"slug": "jab"
}
],
"affiliations_id": [
"5cd84a1fa54d75d1bb45c751"
],
"celestial_objects": null, // <--- Problem here
"celestial_objects_id": [
"5cd9d1fd2fa5dbd585cbdbd9"
],
"coordinates": { ... },
"name": "Solar system",
"slug": "solar-system"
}
]
我希望得到这样的结果:
[
{
"_id": "5cd84eaea54d75d81a94ba0a",
"affiliations": [
{
"_id": "5cd84a1fa54d75d1bb45c751",
"name": "JAB",
"slug": "jab"
} // This is ok
],
"affiliations_id": [
"5cd84a1fa54d75d1bb45c751"
],
"celestial_objects": [ // <--- this should be an array
{
"_id": "5cd9d1fd2fa5dbd585cbdbd9",
"affiliations": [ // <--- linked
{
"_id": "5cd84a1fa54d75d1bb45c751",
"name": "JAB",
"slug": "jab"
}
],
"affiliations_id": [
"5cd84a1fa54d75d1bb45c751"
],
"coordinates": { ... },
"designation": "Planet Earth",
"jump_points_id": [],
"lore": { ... },
"name": "Earth",
"objectType": "PLANET",
"size": 0,
"slug": "earth",
},
{
...
},
],
"celestial_objects_id": [
"5cd9d1fd2fa5dbd585cbdbd9",
"5cd9d1fd2fa5dbd585cbdbd9"
],
"coordinates": { ... },
"name": "Solar system",
"slug": "solar-system"
}
]
编辑:我尝试在聚合结束时创建一个$ group,但是由于“ $ group”,我只能得到一个结果
编辑2 : 这是系统集合中的一条记录:
{
"_id": ObjectID("5cd84eaea54d75d81a94ba0a"),
"name": "Solar System",
"slug": "solar-system",
"coordinates": {
"cartesian": {
"x": 100,
"y": 100,
"z": 100
},
"polar": {
"radius": 173.20508075688772,
"polar": 54.735610317245346,
"azimuth": 45
},
"spheric": {
"radius": 173.20508075688772,
"longitude": 45,
"latitude": 35.264389682754654
}
},
"affiliations_id": [
ObjectID("5cd84a1fa54d75d1bb45c751")
],
"celestial_objects_id": [
ObjectID("5cd9d1fd2fa5dbd585cbdbd9")
]
}
这是celestial_object集合中的一条记录:
{
"_id": ObjectID("5cd9d1fd2fa5dbd585cbdbd9"),
"objectType": "PLANET",
"designation": "Mercury",
"name": "Mercury",
"slug": "mercury",
"coordinates": {
"cartesian": {
"x": 1,
"y": 2,
"z": 3
},
"polar": {
"radius": 3.7416573867739413,
"polar": 36.69922520048988,
"azimuth": 63.43494882292201
},
"spheric": {
"radius": 3.7416573867739413,
"longitude": 63.43494882292201,
"latitude": 53.30077479951012
}
},
"lore": {
"habitable": true,
"population": 10,
"economy": 10,
"danger": 2
},
"size": 0,
"affiliations_id": [
ObjectID("5cd84a1fa54d75d1bb45c751")
],
"jump_points_id": []
}
这是联盟收藏中的记录
{
"_id": ObjectID("5cd84a1fa54d75d1bb45c751"),
"name": "Human",
"slug": "human"
}
给定数据的预期输出:
[
{
"_id": ObjectID("5cd84eaea54d75d81a94ba0a"),
"name": "Solar System",
"slug": "solar-system",
"coordinates": {
"cartesian": {
"x": 100,
"y": 100,
"z": 100
},
"polar": {
"radius": 173.20508075688772,
"polar": 54.735610317245346,
"azimuth": 45
},
"spheric": {
"radius": 173.20508075688772,
"longitude": 45,
"latitude": 35.264389682754654
}
},
"affiliations_id": [
ObjectID("5cd84a1fa54d75d1bb45c751")
],
"affiliations": [ // Affiliations "joined" here
{
"_id": ObjectID("5cd84a1fa54d75d1bb45c751"),
"name": "Human",
"slug": "human"
}
],
"celestial_objects_id": [
ObjectID("5cd9d1fd2fa5dbd585cbdbd9")
],
"celestial_objects": [ // Celestial objects "joined" here
{
"_id": ObjectID("5cd9d1fd2fa5dbd585cbdbd9"),
"objectType": "PLANET",
"designation": "Mercury",
"name": "Mercury",
"slug": "mercury",
"coordinates": {
"cartesian": {
"x": 1,
"y": 2,
"z": 3
},
"polar": {
"radius": 3.7416573867739413,
"polar": 36.69922520048988,
"azimuth": 63.43494882292201
},
"spheric": {
"radius": 3.7416573867739413,
"longitude": 63.43494882292201,
"latitude": 53.30077479951012
}
},
"lore": {
"habitable": true,
"population": 10,
"economy": 10,
"danger": 2
},
"size": 0,
"affiliations_id": [
ObjectID("5cd84a1fa54d75d1bb45c751")
],
"affiliations": [ // Affiliations "joined" here
{
"_id": ObjectID("5cd84a1fa54d75d1bb45c751"),
"name": "Human",
"slug": "human"
}
],
"jump_points_id": []
}
]
}
]