Arup Sarkar
Arup Sarkar

Reputation: 951

Query returning null - Using Java Driver for Mongo

I am trying to update a collection by querying the id of the collection, but for some reason I am not able to update the collection at all. What am I doing wrong?

        DBCollection collection  = db.getCollection("sv_office_src");

        for(OutputSet o : outputData) {
            BasicDBObject searchOfficeQuery = new BasicDBObject();
            searchOfficeQuery.append("_id", o.getId());
            //collection.findOne(searchOfficeQuery);

            System.out.println(searchOfficeQuery);

            BasicDBObject newUpdateDocument = new BasicDBObject();
            newUpdateDocument.append("target_key", o.getTarget_key());
            newUpdateDocument.append("target_office_id", o.getTarget_office_id());
            newUpdateDocument.append("target_firm_id", o.getTarget_firm_id());
            newUpdateDocument.append("score", o.getScore());

            BasicDBObject updatedDocument = new BasicDBObject();
            updatedDocument.append("$set", newUpdateDocument);

            collection.update(searchOfficeQuery, updatedDocument);

            System.out.println("Output : " + updatedDocument);              
        }               

and the output is as follows:

{ "_id" : "52c6f6d5250c7ef0f654c7dd"}

Output :

{ "$set" : { "target_key" : "440786|PO BOX 15007|||WILMINGTON|US-NC|28408-5007|US" , "target_office_id" : "503677" , "target_firm_id" : "87277" , "score" : "17"}}

So I am getting the right document, but in the mongo shell when I am doing the following, you will see that the updated columns are blank. I know the key Firm_Name for the above id.

> db.sv_office_src.find ({Firm_Name: "1717 Capital Management Company"})
{ "_id" : ObjectId("52c77b8d250ca11d792200aa"), "Firm_Name" : "1717 Capital Management Company", "Firm_Id" : "6715", "Office_Id" : "200968", "Office_Address_Line_1" : "PO BOX 15626", "Office_Address_Line_2"
: "", "Office_Address_Line_3" : "", "Office_City" : "WILMINGTON", "Office_Region_Ref_Code" : "US-DE", "Office_Postal_Code" : "19850-5626", "Office_Country_Ref_Code" : "US", "src_key" : "200968|PO BOX 15626||
|WILMINGTON|US-DE|19850-5626|US", "target_key" : "", "target_office_id" : "", "target_firm_id" : "", "target_firm_name" : "", "score" : "" }

Upvotes: 0

Views: 1612

Answers (2)

Rob Moore
Rob Moore

Reputation: 3383

The id's for the two documents are not the same:

{ "_id" : "52c6f6d5250c7ef0f654c7dd"}

vs.

{ "_id" : ObjectId("52c77b8d250ca11d792200aa") <snip/>

Two issue here. The hex values are different and the type of the first one looks to be a "string" and the type if the second is ObjectId.

If your OutputSet.getId() method is returning the hex string then you can convert it to an ObjectId (http://api.mongodb.org/java/current/org/bson/types/ObjectId.html) by passing it to the constructor:

searchOfficeQuery.append("_id", new ObjectId( o.getId() ) );

You can also inspect the WriteResult (http://api.mongodb.org/java/current/com/mongodb/WriteResult.html) from the update command to see how many documents each update updated. Look at the WriteResult.getN() method. In this case I would expect it to be 1 if it finds the document and updates it, zero if it does not find the document.

HTH, Rob.

Upvotes: 1

Eve Freeman
Eve Freeman

Reputation: 33155

The _id in your searchOfficeQuery and the _id in your "proof" from your Firm_Name query don't match. You might try

db.sv_office_src.find({ "_id" : ObjectId("52c6f6d5250c7ef0f654c7dd")})

to see the document that you actually $set all those fields in.

Upvotes: 0

Related Questions