简体   繁体   English

猫鼬独特的条件不起作用

[英]Mongoose distinct condition not working

I'm using mongoose to query a MongoDB using following scheme: 我使用猫鼬使用以下方案查询MongoDB:

var File = mongoose.model('File',{
    size: Number,
    type: String,
    filename: String,
    path: String,
    taxonomies:[{
        tags:[{
            name:String,
            'tag':String
        }],
        name: String
    }]
});

Now I want to do a distinct query as follows: 现在,我想做一个独特的查询,如下所示:

File.distinct('taxonomies.tags.tag', {'taxonomies.name':'AM'},function(error, tags){
     if(err)
           res.send(err);

     console.log("All distinct tags", tags);
})

to get all unique tags from all files where the name of the taxonomy is 'AM'. 从分类名称为“ AM”的所有文件中获取所有唯一标签。 The problem is that the query above returns all tags, including those with a different taxonomies.name. 问题在于上面的查询返回所有标签,包括那些具有不同taxonomies.name的标签。 Without the condition, it just works as it should. 没有条件,它就可以正常工作。

Do I have a syntactic error in here, or am I misunderstanding how distinct works? 我在这里有语法错误吗?还是我误解了不同的工作原理?

Update (More examples) Each document has a taxonomy with name SM and one with name AM, something like 更新(更多示例)每个文档都有一个名称为SM的分类法和一个名称为AM的分类法,例如

taxonomies: [{
            tags: [
            {
                name:"Type",
                'tag':kind
            }, {
                name:"Language",
                'tag':lang
            },
            {
                name:"Code",
                "tag":code
            }],
            name:'AM'
        }, {
            name:'SM',
            tags:[{
                name:"Sales",
                'tag':'has to be sold'
            },{
                name:"Personal filter",
                'tag':'temp'
            }]
        }]

When I execute the query mentioned above, I get as a result: 当我执行上述查询时,得到的结果是:

All distinct tags [ '4007', 'fr', 'has to be sold', 'temp', 'wol', '16104', 'en' ] 所有不同的标签['4007','fr','必须出售','temp','wol','16104','en']

while 'temp' and 'has to be sold are from SM, not AM. 而“ temp”和“必须出售”来自SM,而不是AM。

I want as a result only the ones wehre taxonomies.name = 'AM', across all documents, without duplicates 结果,我只希望在所有文档中使用taxonomies.name ='AM'的文档,而不能重复

The query selection is selecting documents , not elements from the array: 查询选择是在选择文档 ,而不是数组中的元素:

{'taxonomies.name':'AM'}

That criteria will select the entire document. 该条件将选择整个文档。

As a result, all items in both arrays are considered as valid for the distinct operation. 结果,两个数组中的所有项目都被认为对distinct操作有效。 If you only want subsets to be considered, you'll either need to use the aggregation framework to filter, or modify your documents so that the two taxonomies are not stored in the same document. 如果只希望考虑子集,则需要使用聚合框架进行过滤或修改文档,以使两个分类法不会存储在同一文档中。

If you used the aggregation framework instead of the distinct command, there are a lot of options you could use to gather the distinct values: 如果您使用聚合框架而不是distinct命令,则可以使用很多选项来收集distinct值:

db.test.aggregate({$unwind: "$taxonomies" },  
      { $group : 
          {  _id: "$taxonomies.name", 
             tags: { $addToSet: "$taxonomies.tags" } }} )

That should group on both SM and AM (and any others): 这应该在SMAM (以及任何其他)上分组:

 [
     {
             "_id" : "SM",
             "tags" : [
                     [
                             "d1",
                             "e1"
                     ],
                     [
                             "d",
                             "e"
                     ]
             ]
     },
     {
             "_id" : "AM",
             "tags" : [
                     [
                             "a1",
                             "b1",
                             "c1"
                     ],
                     [
                             "a",
                             "b",
                             "c"
                     ]
             ]
     }
]

You could use $match to filter on a particular name as well. 您也可以使用$match来过滤特定name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM