regex as $filter in projection

前端 未结 3 1224
我寻月下人不归
我寻月下人不归 2020-12-20 20:16

I\'m trying to find (using a regexp) an array field and returns that element only

This is my data

  [
 {
\"_id\": \"56d6e8bbf7404bd80a017edb\",
\"nam         


        
3条回答
  •  失恋的感觉
    2020-12-20 21:02

    According this issue Use $regex as the expression in a $cond, the $regex could NOT be used with cond for current mongo version.

    Maybe you can try this one, filter the area3 through $match, then get all matched tags through $group, then remove the _id through $project.

    caseNote.aggregate([{$unwind: '$tags'},
                   {$match: {tags: /area3/}},
                   {$group: {_id: null, tags: {$push: '$tags'}}},
                   {$project: {tags: 1, _id: 0}}])
        .exec(function(err, tags) {
            if (err)
                console.log(err);
            else
                console.log(tags);
        });
    

    Results:

    { "tags" : [ "C area3", "b_area3" ] }
    

提交回复
热议问题