MongoDB中的Java:使用QueryBuilder的$在运营商没有返回蒙戈中查找对象(Mong

2019-06-26 12:37发布

我有一个JUnit rule称为MongoRule样子

public class MongoRule extends ExternalResource {

    private static final Logger LOGGER = LoggerFactory.getLogger(MongoRule.class);
    private final MongoService mongoService;

    public MongoRule() throws UnknownHostException {
        mongoService = new MongoService(getConfiguredHost(), getConfiguredPort(), getConfiguredDatabase());
    }

    @Override
    protected void before() throws Throwable {
        LOGGER.info(" Setting up Mongo Database - " + getConfiguredDatabase());
    }

    @Override
    protected void after() {
        LOGGER.info("Shutting down the Mongo Database - " + getConfiguredDatabase());
        mongoService.getMongo().dropDatabase(getConfiguredDatabase());
    }

    @Nonnull
    public DB getDatabase() {
        return mongoService.getMongo().getDB(getConfiguredDatabase());
    }

    @Nonnull
    public Mongo getMongo() {
        return mongoService.getMongo();
    }

    @Nonnull
    public MongoService getMongoService() {
        return mongoService;
    }

    public static int getConfiguredPort() {
        return Integer.parseInt(System.getProperty("com.db.port", "27017"));
    }

    @Nonnull
    public static String getConfiguredDatabase() {
        return System.getProperty("com.db.database", "database");
    }

    @Nonnull
    public static String getConfiguredHost() {
        return System.getProperty("com.db.host", "127.0.0.1");
    }
}

然后,我尝试将一些文件如下

 public static void saveInDatabase() {
        LOGGER.info("preparing database - saving some documents");
        mongoRule.getMongoService().putDocument(document1);
        mongoRule.getMongoService().putDocument(document2);
    }

document1document2是有效的DBObject文件。 该模式看起来像

{
    Id: 001
    date_created: 2012-10-31
    vars: {
      '1': {
           name: n1
           value:v1
       }
      '2': {
           name: n2
           value:v2
       }
      '3': {
           name: n3
           value:v3
       }

} 
{
    Id: 002
    date_created: 2012-10-30
    vars: {
      '1': {
           name: n4
           value:v4
       }
      '2': {
           name: n5
           value:v5
       }
      '3': {
           name: n6
           value:v6
       }

}

现在,我尝试查询收集和获取这些对象,所以我这样做

public static void getDocuments(List<String> documentIds) {
        BasicDBList docIds = new BasicDBList();
        for (String docId: documentIds) {
            docIds.add(new BasicDBObject().put("Id", docId));
        }
        DBObject query = new BasicDBObject();
        query.put("$in", docIds);
        DBCursor dbCursor = mongoRule.getDatabase().getCollection("mycollection").find(query);
        System.out.println(dbCursor == null);
        if (dbCursor != null) {
            while (dbCursor.hasNext()) {
                System.out.println("object -  " + dbCursor.next());
            }
        }
    }  

mycollection是所有文件都坚持收集,这是来自外部服务。
当我运行这个文件我看到以下

preparing database - saving some documents
inserting document - DBProposal # document1
inserting document - DBProposal # document2
false

这意味着collection.find()无法找到这些文件。

那是什么,我不这样做就在这里? 我怎样才能得到文件回来?

我是很新的使用JavaMongo和使用该参考构造查询

UPDATE
更改查询方式构造后,我还没有看到文件

public static void getDocuments(List<String> documentIds) {
            BasicDBList docIds = new BasicDBList();
            docIds.addAll(documentIds)
            DBObject query = new BasicDBObject();
            query.put("$in", docIds);
            DBCursor dbCursor = mongoRule.getDatabase().getCollection("mycollection").find(query);
            System.out.println(dbCursor == null);
            if (dbCursor != null) {
                while (dbCursor.hasNext()) {
                    System.out.println("object -  " + dbCursor.next());
                }
            }
        } 

和收集名称是通过返回

private static String getCollectionName(@Nonnull final DBObject dbObject) {
        return "mycollection";
    }

Answer 1:

你现在正在做的相当于:

db.col.find({$in:[{Id:id1}, {Id:id2}, ..., {Id:idN}]})

这不是有效的查询,是因为你没有指定哪一个领域到$英寸 我假设你想要的:

db.col.find({Id:{$in:[id1, id2, ..., idN]}})

相应地更改您的查询构造的代码,你应该罚款。

编辑:添加正确的代码:

public static void getDocuments(List<Integer> documentIds) {

            BasicDBList docIds = new BasicDBList();
            docIds.addAll(documentIds)
            DBObject inClause = new BasicDBObject("$in", docIds);
            DBObject query = new BasicDBObject("Id", inClause);
            DBCursor dbCursor = mongoRule.getDatabase().getCollection("mycollection").find(query);
            System.out.println(dbCursor == null);
            if (dbCursor != null) {
                while (dbCursor.hasNext()) {
                    System.out.println("object -  " + dbCursor.next());
                }
            }
        } 

请注意,这是假定“ID”是其他东西比“_id”



文章来源: MongoDB Java: Finding objects in Mongo using QueryBuilder $in operator returns nothing