【问题标题】:How to find documents which contains non empty arrays inside array of items in MongoDB如何在MongoDB的项目数组中查找包含非空数组的文档
【发布时间】:2019-07-31 17:33:41
【问题描述】:

我有一个如下所示的 mongo 文档集合,我正在尝试查找包含该文档中所有颜色的图像的文档,并且文档模板不应为空。

[
    {
        "template" : "one",
        "colors" : [
            { 
                "name" : "yellow",
                images : ["img_one", "image_two"]
            },
            { 
                "name" : "blue",
                images : ["img_one", "image_two"]
            }
        ]
    },
    {
        "template" : "",
        "colors" : [
            { 
                "name" : "green",
                images : ["img_one", "image_two"]
            },
            { 
                "name" : "orange",
                images : ["img_one", "image_two"]
            }
        ]      
    },
    {
        "template" : "three",
        "colors" : [
            { 
                "name" : "green",
                images : ["img_one", "image_two"]
            },
            { 
                "name" : "orange",
                images : []
            }
        ]  
    }
]

我尝试了以下查询,但它不起作用。

db.getCollection('my_products').find({
    "template": {$ne : ""},
    "colors": {
        $all: [
            {
                "$elemMatch": {"images": {$not : {$size : 0}}}
            }
        ]
    }
});

我能做些什么来得到这样的东西?

【问题讨论】:

    标签: mongodb nosql


    【解决方案1】:

    给你...

    db.my_products.aggregate([
        {
            "$match": {
                "template": {
                    "$ne": ""
                },
                "colors": {
                    "$not": {
                        "$elemMatch": {
                            "images": {
                                "$size": 0
                            }
                        }
                    }
                }
            }
        }
    ])
    

    在这里看到它的工作:https://mongoplayground.net/p/ylkdo9AAmZL

    这里是任何感兴趣的人的 c# 对应:

    using MongoDB.Entities;
    using System.Linq;
    
    namespace StackOverflow
    {
        public class Program
        {
            public class product : Entity
            {
                public string template { get; set; }
                public color[] colors { get; set; }
            }
    
            public class color
            {
                public string name { get; set; }
                public string[] images { get; set; }
            }
    
            private static void Main(string[] args)
            {
                new DB("test");
    
                var result = DB.Queryable<product>()
                               .Where(p =>
                                      p.template != "" &&
                                      !p.colors.Any(c => c.images.Count() == 0))
                               .ToList();
            }
        }
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-11
      • 2012-10-01
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      • 2014-05-19
      相关资源
      最近更新 更多