MongoDB查询空,空数组

时间:2018-12-06 14:06:14

标签: arrays mongodb

我需要从数据库中选择available_language为en且featuretags数组不存在,为空或包含给定列表中的元素的所有元素

这是我的过滤器:

 List<FilterDefinition<T>> filters= new List<FilterDefinition<T>>();
        filters.Add(Builders<T>.Filter.Eq("available_languages", language.ToString()));

        if ((activefeatures != null) && (activefeatures.Count > 0))
        {
            List<String> MyActiveFeaturesAsString = new List<string>();
            activefeatures.ForEach(x => MyActiveFeaturesAsString.Add(x.ToString()));

            filters.Add(Builders<T>.Filter.Or(
                    Builders<T>.Filter.Not(Builders<T>.Filter.Exists("featuretags")),
                    Builders<T>.Filter.Size("featuretags", 0),
                    Builders<T>.Filter.In("featuretags", MyActiveFeaturesAsString)));
        }
        else
        {
            filters.Add(Builders<T>.Filter.And(
                    Builders<T>.Filter.Or(
                    Builders<T>.Filter.Not(Builders<T>.Filter.Exists(x => x.FeatureTags)),
                    Builders<T>.Filter.Size(x => x.FeatureTags, 0))));
        }

        FilterDefinition<T> filter= Builders<T>.Filter.And(filters);


 await m_LibraryMongoClient.MongoDbDatabase.GetCollection<T>("collection")
                .Find(filter)
                .ForEachAsync(document =>
                {
                });

过滤器似乎不正确,因为如果我在MongoDB中使用查询,则会得到比使用该过滤器更多的结果。

我在MongoDB

中使用了这个
db.getCollection('collection').find( { $or: [ { featuretags: { $exists: true, $eq: 'something'} }, { featuretags: { $in: [null, [] ]} } ] } )

任何想法都值得赞赏。

0 个答案:

没有答案