MONGODB-QUERY WITH DIFFERENT DOCUMENTS - mongodb

IMAGE OF MY DOCUMENT
This is one of my document with trip detailS.The return ride taking passenger will have his drop latlong of one document equal to pickup lat long of another document.But I am not able to query with taking one field and query with all other documents matching the condition.
{
"_id" : ObjectId("5a1058cd2514972098b5b4e6"),
"trip_id" : 895728,
"pass_id" : 1,
"driver_id" : 5119,
"pick_lat" : 16.863973,
"pick_lon" : 96.11646259999999,
"drop_lat" : 16.806106699999997,
"drop_lon" : 96.15429830000001,
"pickup_date" : ISODate("2017-10-27T13:00:58.000+05:30"),
"drop_date" : ISODate("2017-10-27T14:10:21.000+05:30"),
"distance" : 11.13,
"coordinates" : [
96.11646259999999,
16.863973
],
"type" : "Point",
"
loglat" : {
"type" : "Point",
"coordinates" : [
96.11646259999999,
16.863973
]
}
},

Related

Bounding Box restrictions in MongoDB

I have a collection in mongodb that contains records in the following form:
{
"_id" : ObjectId("60608df8f2119229109345e8"),
"geometry" : {
"type" : "Point",
"coordinates" : [ 23.589316, 37.952651 ]
},
"type" : "Feature",
"properties" : {
"accID" : "100000299",
"timestamp" : "2020-03-01T21:59:04Z"
}
}
I want to create an another collection which contains only the records rely inside a bounding box. The bounding box is defined by some coordinates. More specific the query in PostgreSQL is the following:
CREATE TABLE RecordsFiltered AS
SELECT DISTINCT ON(accID,time) *
FROM OriginalTable
WHERE Longitude BETWEEN 23.0301 and 24.3567 AND Latitude BETWEEN 37.2781 AND 38.4802;

MongoDB delete specific nested element in object array

I want to remove this
"lightControl" : 75
from this
{
"_id" : "dfdfwef-fdfd-fd94284o-aafg",
"name" : "Testing",
"serialNumber" : "fhidfhd8ghfd",
"description" : "",
"point" : {
"type" : "Point",
"coordinates" : [
10.875447277532754,
20.940549069378634
]
},
"ancestors" : [ ],
"metadata" : {
"measurement" : {
"high" : "40000.0",
"medium" : "25000.0"
},
"digitalTwin" : {
},
"emails" : [
""
],
"lastMeasurementDate" : "2010-03-04T11:32:06.691Z",
"lightControl" : 75
},
"tags" : [ ],
"createdAt" : ISODate("2019-12-07T15:22:10.988Z"),
"updatedAt" : ISODate("2020-03-08T15:38:21.736Z"),
"_class" : "com.test.demo.api.model.Device"
}
All I want is for this specific id, to completely delete the lightControl element from metadata. I have tried $pull, but I am probably missing something. Any ideas? Thank you in advance!
Your lightControl is not in array, for nested property, use the dot wrapped in doublequotes:
MongoDB shell:
db.getCollection('yourCollectionName').update({},{$unset:{
"metadata.lightControl":""
}})
In case you have a list of objects with _id(s) instead of updating directly, assume Node.js client for MongoDB is used:
// import mongodb from "mongodb"; // ES6
const mongodb = require("mongodb");
var client = MongoClient(...);
var coll = client["yourDbName"]["yourCollectionName"];
var objs = []; // <-- The array with _id(s)
for (let i=0; i<objs.length; i++){
let id = mongodb.ObjectID(objs[i]["_id"]);
coll.update({ _id:id },{$unset:{ "metadata.lightControl":"" }});
}

How to search nearest place in array object in mongodb

the mongodb document 'contents' is
{
"_id" : ObjectId("57bd1ff410ea3c38386b9194"),
"name" : "4Fingers",
"locations" : [
{
"id" : "locations1",
"address" : "68 Orchard Rd, #B1-07 Plaza Singapura, Plaza Singapura, Singapura 238839",
"phone" : "+65 6338 0631",
"openhours" : "Sunday-Thursday: 11am - 10pm \nFriday/Saturday/Eve of PH*: 11am - 11pm",
"loc" : [
"1.300626",
"103.845061"
]
}
],
"comments" : [ ],
"modified" : 1472271793525,
"created" : 1472012276724,
"createdby" : "Admin",
"modifiedby" : "Admin",
"createdipaddress" : "localhost",
"modifiedipaddress" : null,
"types" : "Restaurant",
"category" : "FoodAndBeverages",
"logo" : "logo4Fingers.png",
"tags" : "western, chicken, restaurant, food, beverages"
}
I want to find the nearest place to my location that i get from HTML5 navigation. How do i query it? Data should be sorted in near to far order.
Thank you.
To query mongodb geospatial data first you need a geo spatial index on your location field.
Your location field is a string, it needs to be a numeric type, you need to update your data accordingly.
Create your index on numerical location data:
db.collection.createIndex( { "locations.loc" : "2d" });
Query:
var projection = {"locations.loc":1};
var query = {"locations.loc": {"$near":[1.300626, 103.845061], "$maxDistance": 0.5}};
db.collection.find(query, projection).pretty();
//result:
{
"_id" : ObjectId("57bd1ff410ea3c38386b9194"),
"locations" : [
{
"loc" : [
1.300626,
103.845061
]
}
]
}
var query2 = {"locations.loc": {"$near":[2.300626, 103.845061], "$maxDistance": 0.5}};
db.collection.find(query2, projection).pretty();
//result:
{}
The query result will be sorted always, with nearest as first document.
Thanks Sergiu Zaharie.
It's working.
Then how can i returned all the field instead of returning this field only.
{
"_id" : ObjectId("57bd1ff410ea3c38386b9194"),
"locations" : [
{
"loc" : [
103.845061,
1.300626
]
}
]
}
//edit
Solved.
i just clear the projection then it work like charm.
Thank you.

how to change mongodb string to geoJSON point throughout the collection

I have a Mongodb collection parking_info and this is one document in the collection:
{
"_id" : ObjectId("559c152fa439a961c357f931"),
"POST_ID" : "354-20160",
"MS_ID" : "-",
"MS_SPACEID" : 0,
"CAP_COLOR" : "Grey",
"METER_TYPE" : "SS",
"SMART_METE" : "Y",
"ACTIVESENS" : "N",
"JURISDICTI" : "SFMTA",
"ON_OFF_STR" : "ON",
"OSP_ID" : 0,
"STREET_NUM" : 2016,
"STREETNAME" : "CHESTNUT ST",
"STREET_SEG" : 3.977e+006,
"RATEAREA" : "Area 5",
"SFPARKAREA" : "Marina",
"LOCATION" : "(37.8007983983, -122.4368696024)"
}
I need to convert the last field "LOCATION" : "(37.8007983983, -122.4368696024)"
here the order is latitude , then longitude.
to
"LOCATION" : { type: "Point", coordinates: [ -122.4368696024, 37.8007983983]},
The coordinate order is longitude, then latitude.
How can I apply that to all documents in the collection?
You can use the cursor method forEach() returned from the find() method to iterate the cursor, access the documents, and with each document modify the LOCATION field by using native JavaScript methods as in the following example:
db.parking_info.find({"LOCATION": {"$type": 2}}).forEach(function(doc){
var obj = {},
loc = [],
temp = doc.LOCATION.replace("(", "").replace(")", "");
loc = temp.split(",").map(function(point){
return parseFloat(point);
});
obj["type"] = "Point";
obj["coordinates"] = loc;
doc.LOCATION = obj;
db.parking_info.save(doc);
});

Query returns wrong result

I've stuck with a problem of matching items inside a polygon (simple box). I can't figure out why the item which is inside the queried box is not resulting. So here what i have:
>db.testing.getIndexes();
{
"0" : {
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test.testing",
"name" : "_id_"
},
"1" : {
"v" : 1,
"key" : {
"point" : "2dsphere"
},
"ns" : "test.testing",
"name" : "2dsphere_index"
}
}
Here is my documents (i've tested different formats):
>db.testing.find();
{
"_id" : ObjectId("5439c9c61120c95f4c50a369"),
"point" : {
"lng" : -80.087535,
"lat" : 42.054246
}
}
{
"_id" : ObjectId("5439cc6d1120c95f4c50a36a"),
"point" : {
"type" : "Point",
"coordinates" : [
-80.087535,
42.054246
]
}
}
And here is query:
>db.testing.find({"point": {"$geoWithin": {
"$geometry": {
"type": "Polygon",
"coordinates": [
[
[-80.267831,42.050312],
[-80.267831,45.003652],
[-73.362579,45.003652],
[-73.362579,42.050312],
[-80.267831,42.050312]
]
]
}
}}})
But the problem is that it returns zero results!
If you not sure, that point is really in square, than copy this:
{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[-80.087535,42.054246]},{"type":"Polygon","coordinates":[[[-80.267831,42.050312],[-80.267831,45.003652],[-73.362579,45.003652],[-73.362579,42.050312],[-80.267831,42.050312]]]}]}
and check it here.
I'm confused, could somebody help me with this please?
Thanks in advance!
Update:
Also, those points is got found when we decrease polygon area, for example, to:
[[-80.159937,42.050312],[-80.05204,42.050312],[-80.05204,42.09646],[-80.159937,42.09646],[-80.159937,42.050312]]
If it's needed i could provide like 100 of such points. For example one more strange point:
{"type":"Point","coordinates":[-76.537071,42.058731]}
Update:
Here is a file with points collection dump (around 700 points).
MongoDB (2d sphere index) uses Geodesic to build polygons, which means the shortest line between two points.
This is how it looks on sphere:
And this is how it looks on plain:
Zoomed in image shows that point really not in square: