0

I have this schema:

{
    "_id" : UUID("f61dacb9-7687-40ea-8c23-b1d7364a95f8"),
    "Lat" : "39.74719833333334",
    "Lng" : "57.2768",
    "CreationDateTime" : ISODate("2017-11-11T08:06:42.729Z"),
    "DeviceId" : "89984320000897548912",
    "UserId" : UUID("7bc16d9a-9ac4-47af-bc28-adad1622a054"),
    "UserName" : "admin",

},...

As you can see i have this fields: Lat and Lng.Now i have decided these fields combine together as a array filed like below :

    {
        "_id" : UUID("f61dacb9-7687-40ea-8c23-b1d7364a95f8"),
        "Lat" : "39.74719833333334",
        "Lng" : "57.2768",
        "CreationDateTime" : ISODate("2017-11-11T08:06:42.729Z"),
        "DeviceId" : "89984320000897548912",
        "UserId" : UUID("7bc16d9a-9ac4-47af-bc28-adad1622a054"),
        "UserName" : "admin",
        "Point" : [ 
           57.2768, 
           39.74719833333334
        ] ,
},....

For this purpose i wrote this query:

db.Locations.find().forEach(function(data){
    up(data["_id"],data["Lng"], data["Lat"]);
});
var up = function (id,lng,lat){
    db.Locations.updateOne(
        {_id : id},
        { $addToSet: {Point: [ lng, lat ] } }
    );
}  

I have used addToSet for this query, But it does not have right answer.In my query just first field is updated like this:

{...
"Point" : [ 
        [ 
            UUID("13fe657e-6f39-4396-a6d1-dc70ddf38201"), 
            "49.996898333333334"
        ], 
        [ 
            UUID("99aa3773-a444-48cb-80ae-958181633fd9"), 
            "49.998398333333334"
        ],...
}

It does not my thing that i want??Where is my mistake? First i make a query and then i have updated document by document!!!! Thank you for helping...

Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149
  • You simply want `$set` here and not `$addToSet`. You really should be doing something like `{ "$set": { "location": { "type": "Point", "coordinates": [lng, lat] } } }` anyway as that is actually a supported GeoJSON format for usage with geospatial query operations with MongoDB. – Neil Lunn May 28 '18 at 05:05
  • Yet i do not want to use GeoJSON.I want to use `2D Indexes`. I will check `set` dude. @NeilLunn – Cyrus the Great May 28 '18 at 05:09
  • dude by set i can not create this : Point[lng,lat]. I have changed my query : http://codepad.org/lXiMDpNi but i still got same problem @NeilLunn – Cyrus the Great May 28 '18 at 05:20
  • Look at the large prominent links above your question. There are more than just a few direct examples for you to follow. Those are there to help you, so you should be using them. – Neil Lunn May 28 '18 at 05:26
  • @NeilLunn Thank you man. Your are always help to me .Thank you again – Cyrus the Great May 28 '18 at 05:39

0 Answers0