Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7490705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:43:02+00:00 2026-05-29T15:43:02+00:00

Disclaimer: I’m a noob to Mongo and to databases in general, so if I’m

  • 0

Disclaimer: I’m a noob to Mongo and to databases in general, so if I’m off on terminology or concepts please let me know.

For the purposes of this example, I’d like to store the mouse coordinates together within an object nested within a document. That document would look something like:

{ "_id" : ObjectId("4f39805d35919a2a7c7aba3e"), "mouseLoc" : { "x" : 10, "y" : 20 } }

It seems like atomic updating, via modifier operations, is the way to go, as I’m only storing values here (retrieving them elsewhere, in other software). However, I can’t figure out the query part. How do I access this document in order to set the x and y values?

I’m using Java, so I’m currently trying:

BasicDBObject mouseLoc = new BasicDBObject();
mouseLoc.put("x", mouseX);
mouseLoc.put("y", mouseY);
myCollection.update(queryObj, new BasicDBObject("$set", mouseLoc), true, false);

However, I don’t know how to specify that queryObj to get the document with the mouseLoc key. Alternatively, if there’s a smarter way to store information like this, please school me.

I can follow along with Mongo shell/JS tips if that’s simpler.

UPDATE:
I can query for this document if I give it a static field like ‘name’. So, to update this document:

{ "_id" : ObjectId("4f39805d35919a2a7c7aba3e"), "name" : "mouseLoc", "mouseLoc" : { "x" : 10, "y" : 20 } }

I can use this code:

BasicDBObject mouseLoc = new BasicDBObject();
mouseLoc.put("x", mouseX);
mouseLoc.put("y", mouseY);

BasicDBObject queryObj = new BasicDBObject("name", "mouseLoc");
BasicDBObject updateObj = new BasicDBObject("$set", new BasicDBObject("mouseLoc", mouseLoc));

myCollection.update(queryObj, updateObj, true, false);

However, it seems redundant to have to specify that “name” field just to be able to retrieve the document with the “mouseLoc” key. Maybe I’m just misunderstanding database / mongo design?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:43:04+00:00Added an answer on May 29, 2026 at 3:43 pm

    The query part is where you specify a way to find you document, e.g: the _id or another unique field.

    In your example, it will look like this:

    ObjectId objectId = new ObjectId("4f39805d35919a2a7c7aba3e");
    BasicDBObject queryObj = new BasicDBObject("_id", objectId);
    

    (Maybe there’s something missing about ObjectId, since I don’t know Java.)

    UPDATE

    If you are searching for a specific mouseLoc, the queryObj will be the very mouseLoc object. (In JSON it would be { 'x': mouseX, 'y': mouseY }.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: I'm very new to SQL and databases in general. I need to create
Disclaimer: I know this type of question has been asked here before, I just
Disclaimer: not sure this is WordPress related or not. I'm following a simple tutorial
Disclaimer: I know there are better ways to track email open rates, which are
Disclaimer: this question is driven by my personal curiosity more than an actual need
Disclaimer : I kept this because some things may be useful to others, however,
Disclaimer : I'm an SSRS n00b, so not too many rotten tomatoes please :)
Disclaimer, I'm not a PHP programmer, so you might find this question trivial. That's
Disclaimer : I already asked this question , but without the deployment requirement. I
Disclaimer : this is a strange issue that only happens in a Kindle Fire

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.