MongoDB Java: Das Suchen von Objekten in Mongo mit dem Operator QueryBuilder $ in gibt nichts zurück
Ich habe einJUnit rule
genannt alsMongoRule
sieht aus wie
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");
}
}
Dann versuche ich einige Dokumente wie folgt einzufügen
public static void saveInDatabase() {
LOGGER.info("preparing database - saving some documents");
mongoRule.getMongoService().putDocument(document1);
mongoRule.getMongoService().putDocument(document2);
}
Woherdocument1
unddocument2
sind gültigDBObject
Unterlagen. Das Schema sieht aus wie
{
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
}
}
Jetzt versuche ich, die Auflistung abzufragen und diese Objekte abzurufen
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
Ist die Sammlung, in der alle Dokumente aufbewahrt werden, stammt diese von einem externen Dienstleister.
Wenn ich dieses Dokument ausführe, sehe ich Folgendes
preparing database - saving some documents
inserting document - DBProposal # document1
inserting document - DBProposal # document2
false
Was bedeutetcollection.find()
konnte diese Dokumente nicht finden.
Was mache ich hier nicht richtig? Wie kann ich die Dokumente zurückbekommen?
Ich bin sehr neu im Umgang mitJava
mitMongo
und benutzte diesReferenz um die Abfrage zu konstruieren
AKTUALISIEREN
Nach einer Änderung der Art und Weise, in der die Abfrage erstellt wird, werden keine Dokumente angezeigt
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());
}
}
}
und der Name der Sammlung wird über zurückgegeben
private static String getCollectionName(@Nonnull final DBObject dbObject) {
return "mycollection";
}