【问题标题】:MongoDb Aggregation $in over list of collectionMongoDb 聚合 $in 超过集合列表
【发布时间】:2016-10-13 18:02:16
【问题描述】:
BasicDBObject matchDBObejct = new BasicDBObject('$match',
            new BasicDBObject("type", ArticleSourceType.MICROBLOGS.toString())
           .append(trackInsightGraphFilterCO.getMetricFeild(), new BasicDBObject('$exists', true).append('$ne', ""))
    )
BasicDBObject groupDBObject = new BasicDBObject('$group', new BasicDBObject("_id", '$' + trackInsightGraphFilterCO.getMetricFeild()).append("total", new BasicDBObject('$sum', 1)))
BasicDBObject limitDBObject = new BasicDBObject('$sort', new BasicDBObject("total", -1))
BasicDBObject sortDbObject = new BasicDBObject('$limit', 10);
List<BasicDBObject> pipeline = [matchDBObejct, groupDBObject, limitDBObject, sortDbObject];

我上面的查询运行良好- 但是当我在匹配查询中添加其他参数时,我没有得到任何结果。

.append("track",new BasicDBObject('$in',tracks))

tracks 是 Track 域的列表 .toString(); 即-

List<Track> trackDomainObjects;
for(Track track:trackDomainObjects){
tracks.add(track.toString())
}

我的匹配查询变成了这样-

BasicDBObject matchDBObejct = new BasicDBObject('$match',
            new BasicDBObject("type", ArticleSourceType.MICROBLOGS.toString())
                    .append("track",new BasicDBObject('$in',tracks))
                    .append(trackInsightGraphFilterCO.getMetricFeild(), new BasicDBObject('$exists', true).append('$ne', ""))
    )

控制台没有异常,我的 Mongo DB 中有数据。 我下面的控制台查询给了我数据-

db.socialNotification.aggregate([ 
{$match:{"type":"MICROBLOGS","track":{$in:[DBRef('track',ObjectId("57f5fe0ce4b0e960f6d6ab9a")),DBRef('track',ObjectId("57f5fe2be4b0e960f6d6ac42"))]},"authorLocation" : { $exists : true, $ne : "" } }}, 
{ $group: { _id: "$authorLocation", total: { $sum: 1 } } }, 
{$sort:{ total:-1}},
{$limit: 10} ])

【问题讨论】:

    标签: mongodb mongodb-query spring-data aggregation-framework


    【解决方案1】:

    从你的问题我读到:

    where tracks is list of Track domain .toString(); ie-
    

    但在您的工作示例中,我看到了:

    {$match:{"type":"MICROBLOGS","track":{$in:[DBRef('track',ObjectId("57f5fe0ce4b0e960f6d6ab9a")),DBRef('track',ObjectId("57f5fe2be4b0e960f6d6ac42"))]},"authorLocation" : { $exists : true, $ne : "" } }}, 
    

    在您的工作示例中,您有一个 ObjectId 列表。 确保代码中的“tracks”也是ObjectIds 列表!当您传递字符串列表时,它将不起作用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-26
      • 2020-04-25
      • 2014-05-27
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 2019-02-25
      相关资源
      最近更新 更多