Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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. </p> <p>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: </p> <pre><code>{ "$set" : { "status" : 0} , "pos.X" : "90.45" , "pos.Y" : "90.45" , "pos.Z" : "90.45"} </code></pre> <p>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:</p> <pre><code>"pos" : { "X" : "0", "Y" : "0", "Z" : "0" } </code></pre> <p>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: <a href="http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29" rel="nofollow">http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29</a></p> <p>Here is an Update statement that will modify the above values:</p> <pre><code>&gt; 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" } &gt; db.pseudo.update({"pseudo" : "gPlayer"}, { "$set" : { "status" : 0 , "pos.0.X" : "0" , "pos.1.Y" : "0" , "pos.2.Z" : "0"}}) &gt; db.pseudo.find({ "pseudo" : "gPlayer"}).pretty() { "_id" : ObjectId("4f904ebb5bebd4375b759c90"), "email" : "email", "password" : "password", "pos" : [ { "X" : "0" }, { "Y" : "0" }, { "Z" : "0" } ], "pseudo" : "gPlayer", "status" : 0 } &gt; </code></pre> <p>Translated into Java this is:</p> <pre><code>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); </code></pre> <p>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!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload