Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

database - MongoDb sort array values not working in aggregation pipeline

consider the following collection:

{"productId" : 1,
"isVariant": 1,
"isComplete" : 1,
"variantId" : 1,
"attributeSet" : [ 
        {
            "name" : "Capacity",
            "value" : "500 GB",
            "id" : 3
        }, 
        {
            "name" : "Form Factor",
            "value" : "5 inch",
            "id" : 4
        }, 
        {
            "id" : 5,
            "name" : "Memory Components",
            "value" : "3D NAND",
            "isVariation" : 0
        }
    ]
},
{"productId" : 2,
"isVariant": 1,
"isComplete" : 1,
"variantId" : 1,
"attributeSet" : [ 
        {
            "name" : "Capacity",
            "value" : "1 TB",
            "id" : 3
        }, 
        {
            "name" : "Form Factor",
            "value" : "5 inch",
            "id" : 4
        }, 
        {
            "id" : 5,
            "name" : "Memory Components",
            "value" : "3D NAND",
            "isVariation" : 0
        }
    ]
},
{"productId" : 3,
"isVariant": 1,
"isComplete" : 0,
"variantId" : 1,
"attributeSet" : [ 
        {
            "name" : "Capacity",
            "value" : "500 GB",
            "id" : 3
        }, 
        {
            "name" : "Form Factor",
            "value" : "2.5 inch",
            "id" : 4
        }, 
        {
            "id" : 5,
            "name" : "Memory Components",
            "value" : "3D NAND",
            "isVariation" : 0
        }
    ]
},
{"productId" : 4,
"isVariant": 1,
"isComplete" : 0,
"variantId" : 1,
"attributeSet" : [ 
        {
            "name" : "Capacity",
            "value" : "1 TB",
            "id" : 3
        }, 
        {
            "name" : "Form Factor",
            "value" : "2.5 inch",
            "id" : 4
        }, 
        {
            "id" : 5,
            "name" : "Memory Components",
            "value" : "3D NAND",
            "isVariation" : 0
        }
    ]
}

Here's my query for returning the values of each attribute set values that are not variation.

db.collection.aggregate([
  { $match: { isComplete: 1 } },
  {
    $project: {
      _id: 0,
      attributeSet: 1
    }
  },
  { $unwind: "$attributeSet" },
  { $match: { "attributeSet.isVariation": { $ne: 0 } } },
  {
    $group: {
      _id: "$attributeSet.id",
      name: { $first: "$attributeSet.name"  },
      values: { $addToSet: "$attributeSet.value" }
    }
  },
{ 
 $sort: {
   name : 1,
   values : 1
 }
}
])

The above query works fine and resulting the desired output but the only problem is while sorting the values. The sort stage works fine for name but the values which is an array does not get sorted. Can anyone help me with this query?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...