java mongodb $in_MongoDB Java:使用QueryBuilder $in运算符在Mongo中查找对象不返回任何内容...

我有一个名为MongoRule的JUnit规则

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);

}

其中document1和document2是有效的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 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()无法找到这些文档.

我在这里做的不是什么?我该如何取回文件?

我非常擅长将Java与Mongo一起使用,并使用这个reference构建查询

UPDATE

更改构建查询的方式后,我仍然看不到文档

public static void getDocuments(List 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";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值