Metra
Metra

Reputation: 393

Update data with mongoDB in Java

I have a little bug with my script:

   BasicDBObject change = new BasicDBObject();   
   BasicDBObject account =
     new BasicDBObject().append("$set", new BasicDBObject().append("status", 0));
   account.append("pos.X", getX());                                        
   account.append("pos.Y", getY());                                      
   account.append("pos.Z", getZ());                                          
   change.append("pseudo", gPlayer);
   coll.update(change, account);

And the structure of mongoDB is :

{
      pseudo: "pseudo"
      email: "email"
      password: "password"
      status: "1"
      pos: [
        {X: "90.45}
        {Y: "90.45}
        {Z: "90.45}
      ]

}

But this is not working! No value is modified.

Thanks for your help.

Upvotes: 1

Views: 3989

Answers (1)

Marc
Marc

Reputation: 5548

Java is a very verbose language, and sometimes it is easiest to first write your query using the JS shell, make sure it performs as desired, and then translate it into Java.

By adding System.out.println(account.toString()) to your Java code, I can see that your Update document looks like the following, which is not valid:

{ "$set" : { "status" : 0} , "pos.X" : "90.45" , "pos.Y" : "90.45" , "pos.Z" : "90.45"}

From your question, it is not entirely clear what you would like your updated document to look like, but I am guessing that you would like to modify the values of "status" and "pos.0.X", "pos.1.Y", and "pos.2.Z". Because X, Y, and Z are all stored as individual embedded documents inside an array, they will have to be referenced by their positions in order to be updated at the same time. If possible, you might find it preferable to rearrange your document structure such that X, Y, and Z are all stored inside the same document, like so:

"pos" : {
            "X" : "0", 
            "Y" : "0",
            "Z" : "0"
        }

This way, you will be able to access each variable more easily using dot notation, pos.X, pos.Y, pos.Z, which from your post looks like what you were intending. More information on embedded documents may be found in the "Dot Notation (Reaching into Objects)" documentation: http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29

Here is an Update statement that will modify the above values:

> db.pseudo.find({ "pseudo" : "gPlayer"}).pretty()
{
    "_id" : ObjectId("4f904ebb5bebd4375b759c90"),
    "email" : "email",
    "password" : "password",
    "pos" : [
        {
            "X" : "90.45"
        },
        {
            "Y" : "90.45"
        },
        {
            "Z" : "90.45"
        }
    ],
    "pseudo" : "gPlayer",
    "status" : "1"
}
> db.pseudo.update({"pseudo" : "gPlayer"}, { "$set" : { "status" : 0 , "pos.0.X" : "0" , "pos.1.Y" : "0" , "pos.2.Z" : "0"}})
> db.pseudo.find({ "pseudo" : "gPlayer"}).pretty()
{
    "_id" : ObjectId("4f904ebb5bebd4375b759c90"),
    "email" : "email",
    "password" : "password",
    "pos" : [
        {
            "X" : "0"
        },
        {
            "Y" : "0"
        },
        {
            "Z" : "0"
        }
    ],
    "pseudo" : "gPlayer",
    "status" : 0
}
> 

Translated into Java this is:

BasicDBObject change = new BasicDBObject("pseudo", "gPlayer");   
BasicDBObject setDoc = new BasicDBObject();                 
setDoc.append("status", "0"); 
setDoc.append("pos.0.X", "0");                                        
setDoc.append("pos.1.Y", "0");                                      
setDoc.append("pos.2.Z", "0");                                          
BasicDBObject account = new BasicDBObject("$set", setDoc);
coll.update(change, account);

I realize that I guessed a little bit about exactly the update that you would like to do, but hopefully the above will get you pointed in the right direction!

Upvotes: 3

Related Questions