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 8305121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:00:39+00:00 2026-06-08T18:00:39+00:00

I want to store a simple array of latitude,longitude inside a mongoDB collection using

  • 0

I want to store a simple array of latitude,longitude inside a mongoDB collection using java and I am using following code but my “loc” array always is empty :

BasicDBObject doc = new BasicDBObject();
//doc.put("key " , r.getKey());
doc.put("category" , r.getKey());
doc.put("type" , r.getValue());

BasicDBObject latlon = new BasicDBObject();


ArrayList<Float> aLatLong = new ArrayList<Float>();

{




    Float fLat = Float.parseFloat(r.getLatitude());
    Float fLon = Float.parseFloat(r.getLongitude());
    aLatLong.add(fLat);
    aLatLong.add(fLon);

    latlon.put("lat" , fLat);
    latlon.put("lon" , fLon);
}


doc.put( "loc" , aLatLong);

but when I don’t use ArrayList at all and try to store latlon into “loc” it will show me the data but its not an array anymore.

how can I save a simple array in mongoDB?

  • 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-06-08T18:00:41+00:00Added an answer on June 8, 2026 at 6:00 pm

    Try this

    BasicDBObject doc = new BasicDBObject();
    //doc.put("key " , r.getKey());
    doc.put("category" , r.getKey());
    doc.put("type" , r.getValue());
    
    java.util.Map<String,Float> latLongMap = new java.util.HashMap<String,Float>();
    latLongMap.put("lat" , Float.parseFloat(r.getLatitude());
    latLongMap.put("lon" , Float.parseFloat(r.getLongitude());
    
    doc.put( "location" , latLongMap);
    

    This will store a json of lat-long. Also if you want to store them as array, that is also possible but i will suggest you store them as this.

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

Sidebar

Related Questions

I'll be very simple: I want to be able to store Java objects from
I want to save and store simple mail objects via serializing, but I get
he guys, i want to make a simple reference on mongodb documents using symfony2.
I want to use ARC in my simple class where I store some values
I want to store data from form named adding in an array - I
I made a simple cookie and i want to store a python stack object.
I'm trying to do something fairly simple, but it's proving quite tricky. I want
I have a simple requirement (perhaps hypothetical): I want to store english word dictionary
my coding is working well .But I want to store the edited spinner value
Simple requirement: I want to store a flat unchanged XML strings into a MySQL-DB

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.