4

I have a JUnit rule called as MongoRule looks like

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

Then I try to insert some documents as following

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

Where document1 and document2 are valid DBObject documents. The schema looks like

{
    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
       }

}

Now I try to query the collection and get these objects, so I do this

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 is the collection where all documents are persisted, this comes from an external service.
When I run this document I see following

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

Which means collection.find() could not find these documents.

What is that I am not doing right here? How can I get the documents back?

I am very new to using Java with Mongo and used this reference to construct the query

UPDATE
After changing the way query is constructed, I still don't see documents

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

and the collection Name is returned via

private static String getCollectionName(@Nonnull final DBObject dbObject) {
        return "mycollection";
    }
5
  • Can you post your putDocument code as well - just making sure the collection names are the same :) Commented Jul 25, 2012 at 15:18
  • Also ensure you are using a write concern at least SAFE or the writes might not have been committed to memory before the find is executing meaning it will not find any documents. Commented Jul 25, 2012 at 15:35
  • It's not a valid query. I'll post a response. Commented Jul 25, 2012 at 15:38
  • @christkv, yes the default write concern is WriteConcern.SAFE Commented Jul 25, 2012 at 15:50
  • @Ross, I added the code that returns collection name Commented Jul 25, 2012 at 15:54

1 Answer 1

10

You are now doing the equivalent of :

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

Which is not a valid query since you're not specifying what field to $in on. I'm assuming you want :

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

Change your query construction code accordingly and you should be fine.

EDIT: Adding correct code :

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

Please note that this assumes "Id" is something other than "_id"

Sign up to request clarification or add additional context in comments.

3 Comments

The code is still wrong. And add a sample document, your schema description is ambiguous.
I updated the schema, thank you for helping out, I am still not able to figure out how to do this correctly
Have you tried my code? It works provided you don't provide a list of String put Integer (your schema makes "Id" fields an Integer rather than a String).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.