【问题标题】:MongoClient $in doesn't work with int arrayMongoClient $in 不适用于 int 数组
【发布时间】:2018-06-28 07:50:10
【问题描述】:

我是 mongodb 的新手,我有一个这样的测试集合:

 { "_id" : ObjectId("5afce40ae1def619f8c591f1"), "location" : "shanghai", "version" : "1.14", "platform" : 1 }
{ "_id" : ObjectId("5afce43ce1def619f8c591f3"), "location" : "beijing", "version" : "1.2", "platform" : 1 }

它在 shell 中运行良好:

db.test.aggregate([{$match: { 'location':{ $in:['beijing','shanghai'] }  , 'platform':{ $in:[1]}  }}])

它确实可以像这样在 java MongoClient 中工作:

 List<Bson> filter = Arrays.asList(
            Document.parse("{$match: { 'location':{ $in:['beijing','shanghai'] }  }}")
        );
 AggregateIterable<Document> doc = mongoClient.getDatabase(db).getCollection(collectionName).aggregate(filter);

当 $in 使用 int 数组时,它不起作用并返回一个空数组:

 List<Bson> filter = Arrays.asList(
            Document.parse("{$match: { 'location':{ $in:['beijing','shanghai'] }  , 'platform':{ $in:[1]}  }}")
        );
 AggregateIterable<Document> doc = mongoClient.getDatabase(db).getCollection(collectionName).aggregate(filter);

【问题讨论】:

  • 帮助 ~-~ ~-~ ~-~
  • 它的工作......

标签: mongodb mongodb-query mongodb-java


【解决方案1】:

试试这个

MongoCollection<Document> doc = db.getCollection("TestCollection1");
        List<Bson> filter = Arrays.asList(
                Document.parse("{$match: { 'location':{ $in:['beijing','shanghai'] }  , 'platform':{ $in:[1]}  }}")
            );
        AggregateIterable<Document> doc2= doc.aggregate(filter);
        Iterator iterator = doc2.iterator(); 
        while(iterator.hasNext()){
            Document plant = (Document) iterator.next();
         System.out.println(plant);
        }

输出

文档{{_id=5afce40ae1def619f8c591f1, location=shanghai, version=1.14, platform=1.0}} 文档{{_id=5afce43ce1def619f8c591f3, location=beijing, version=1.2, platform=1.0}}

【讨论】:

  • 还是不行..起初我以为是mongo客户端和服务器的版本冲突引起的,刚才我把mongo-java-driver版本切换到3.2.0也是一样的作为 mongo 服务器,但结果是一样的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多