While trying to setup to use the Geospatial Index on MongoDB, I run into the error message that the location array is not in the correct format.
This is my collection "test".
{
"_id" : ObjectId("4f037ac176d6fdab5b00000a"),
"CorporateId" : "XYZ12345",
"Places" : [
{
"Location" : {
"Longitude" : "50.0",
"Latitude" : "50.0"
},
"ValidFrom" : "2011-11-01 00:00:00",
"ValidTo" : "2021-12-31 00:00:00",
"itemCount" : "1"
}
]
}
Once I run this code.
db.test.ensureIndex({"Places.Location": "2d"});
I get this error message
location object expected, location array not in correct format
My assumption is that the Long/Lat needs to be a number.
Currently it is an object.
typeof db.test.Places.Location.Longitude -> Object
typeof db.test.Places.Location -> Object
My problem is that since I am still quite new to MongoDB, I don't really know how to approach this problem in the best way.
Mongodb expects numbers for the coordinates while you passed in a string.
"Location" : {
"Longitude" : 50.0, // <<<<<<<<<<<<<< use numbers instead
"Latitude" : 50.0
},
see http://www.mongodb.org/display/DOCS/Geospatial+Indexing for details.
The problem has been fixed by converting the Location parameters into a float type like this.
$var = $JSonString['Places'];
$test=count($var);
$i=0;
for ( $i=0; $i<$test;$i++){
$lon = (float)$JSonString['Places'][$i]['Location']['Longitude'];
$lat = (float)$JSonString['Places'][$i]['Location']['Latitude'];
$JSonString['Places'][$i]['Location']['Longitude'] =$lon ;
$JSonString['Places'][$i]['Location']['Latitude'] =$lat ;
//error_log($lon . "->".gettype($JSonString['Places'][$i]['Location']['Latitude']), 3 , "/var/tmp/my-errors.log");
}
Related
I am using yii2 mongodb latest version, when i try to get record in a given date range i get null records. my codes are as follow
Function that filter records received today that is in my model
public function today(){
$finder = self::find();
$startDay = strtotime('midnight',time())-1;//start of day
$endDay = time(); //now
$d1=to_isoDate($startDay);
$d2=to_isoDate($endDay);
$args=['created_at'=>['$gte'=>$d1,'$lte'=>$d2]];
$finder->andWhere($args);
return $finder;
}
Function that converts time-stamp to UTCDateTime, when inserting to collection or create a query i call this function
function to_isoDate($timestamp){
return new \MongoDB\BSON\UTCDateTime($timestamp);
}
Trying to get all models that were created today returns nothing yet i have records for the day
One of the mongo document is as follow
{
"_id" : ObjectId("5892deb01c80f22a180af457"),
"title" : "TEST",
"content" : "we are",
"slug" : "test",
"guid" : "5892deae76036",
"type" : "sms_outbox",
"mime" : ObjectId("5891ae441c80f24e4057f332"),
"meta" : {
"from" : "5891d0a51c80f2131d327b92",
"scheduled" : "0"
},
"alias" : "+254723681977",
"parent" : "5891ae071c80f2394e688db5",
"status" : "sent",
"created_at" : ISODate("2017-01-01T07:24:30.000+0000"),
"updated_at" : ISODate("2017-02-02T07:24:30.000+0000"),
"updated_by" : "588899ac1c80f227512d1102",
"created_by" : "588899ac1c80f227512d1102"
}
Kindly assist to troubleshoot what am doing wrong
I had a similar issue today, multiplying the $timestamp by 1000 resolved the issue and the date filter works fine now.
Here is the modified to_isoDate method:
function toIsoDate($timestamp){
return new \MongoDB\BSON\UTCDateTime($timestamp * 1000);
}
You also need to modify your query to look like this:
public function today(){
$finder = self::find();
$startDay = strtotime('midnight',time())-1;//start of day
$endDay = time(); //now
$d1= toIsoDate($startDay);
$d2= toIsoDate($endDay);
$finder->andWhere(['>=', 'created_at', $d1]);
$finder->andWhere(['<=', 'created_at', $d2]);
return $finder;
}
Reference: https://github.com/yiisoft/yii2-mongodb/issues/181
I hope this helps someone, I spent quite some time trying to find a solution to this.
I am attempting to build a query to run from Mongo client that will allow access to the following element of a hash within a hash within a hash.
Here is the structure of the data:
"_id" : ObjectId("BSONID"),
"e1" : "value",
"e2" : "value",
"e3" : "value"),
"updated_at" : ISODate("2015-08-31T21:04:37.669Z"),
"created_at" : ISODate("2015-01-05T07:20:17.833Z"),
"e4" : 62,
"e5" : {
"sube1" : {
"26444745" : {
"subsube1" : "value",
"subsube2" : "value",
"subsube3" : "value I am looking for",
"subsube4" : "value",
"subsube5" : "value"
},
"40937803" : {
"subsube1" : "value",
"subsube2" : "value",
"subsube3" : "value I am looking for",
"subsube4" : "value",
"subsube5" : "value"
},
"YCPGF5SRTJV2TVVF" : {
"subsube1" : "value",
"subsube2" : "value",
"subsube3" : "value I am looking for",
"subsube4" : "value",
"subsube5" : "value"
}
}
}
So I have tried dotted notation based on a suggestion for "diving" into an wildcard named hash using db.my_collection.find({"e5.sube1.subsube4": "value I am looking for"}) which keeps coming back with an empty result set. I have also tried the find using a match instead of an exact value using /value I am lo/ and still an empty result set. I know there is at least 1 document which has the "value I am looking for".
Any ideas - note I am restricted to using the Mongo shell client.
Thanks.
So since this is not capable of being made into a javascript/mongo shell array I will go to plan B which is write some code be it Perl or Ruby and pull the result set into an array of hashes and walk each document/sub-document.
Thanks Mario for the help.
You have two issues:
You're missing one level.
You are checking subsube4 instead of subsube3
Depending on what subdocument of sube1 you want to check, you should do
db.my_collection.find({"e5.sube1.26444745.subsube4": "value I am looking for"})
or
db.my_collection.find({"e5.sube1.40937803.subsube4": "value I am looking for"})
or
db.my_collection.find({"e5.sube1.YCPGF5SRTJV2TVVF.subsube4": "value I am looking for"})
You could use the $or operator if you want to look in any one of the three.
If you don't know the keys of your documents, that's an issue with your schema design: you should use arrays instead of objects. Similar case: How to query a dynamic key - mongodb schema design
EDIT
Since you explain that you have a special request to know the count of "value I am looking for" only one time, we can run a map reduce. You can run those commands in the shell.
Define map function
var iurMapFunction = function() {
for (var key in this.e5.sube1) {
if (this.e5.sube1[key].subsube3 == "value I am looking for") {
var value = {
count: 1,
subkey: key
}
emit(key, value);
}
}
};
Define reduce function
var iurReduceFunction = function(keys, countObjVals) {
reducedVal = {
count: 0
};
for (var idx = 0; idx < countObjVals.length; idx++) {
reducedVal.count += countObjVals[idx].count;
}
return reducedVal;
};
Run mapreduce command
db.my_collection.mapReduce(iurMapFunction,
iurReduceFunction, {
out: {
replace: "map_reduce_result"
},
}
);
Find your counts
db.map_reduce_result.find()
This should give you, for each dynamic key in your object, the number of times it had an embedded field subsube3 with value value I am looking for.
Sample json object :
{ "_id" : ObjectId( "55887982498e2bef5a5f96db" ),
"a" : "x",
"q" : "null",
"p" : "",
"s" : "{\"f\":{\"b\":[\"I\"]},\"time\":\"fs\"}" }
need all documents where time = fs
My query :
{"s":{"time" : "fs"}}
above returns zero products but that is not true.
There are two problems here. First of all s is clearly a string so your query cannot work. You can use $regex as below but it won't be very efficient:
{s: {$regex: '"time"\:"fs"'}}
I would suggest converting s fields to proper documents. You can use JSON.parse to do it. Documents can be updated based on a current value using db.foo.find().snapshot().forEach. See this answer for details.
Second problem is your query is simply wrong. To match time field you should use dot notation:
{"s.time" : "fs"})
I have a collection of emails in MongoDB with a field containing an array of json.
How can knowing the sender and the receiver, how can I find all the emails exchanged between the two people ? I need to do something like
db.email.find({"from": i.st20#gmail.com", "tos":"ron#gmail.com")
but I cant find the right way to write this query :(
> db.emails.findOne()
{
"from" : {
"real_name" : "it",
"address" : "i.st20#gmail.com"
},
"tos" : [
{
"real_name" : null,
"address" : "ron#gmail.com"
}
],
}
Use "from.address" and "tos.address":
db.emails.find({"from.address" : "i.st20#gmail.com", "tos.address" : "ron#gmail.com"})
Each field is considered as a json, we can precise the expected value through a "." :
db.emails.find({"tos.address" : "ron#gmail.com", "from.address":"i.st20#gmail.com"})
From and To are objects and data inside them can be accessed through a . operator.
So the query will be:
db.emails.find({"from.address" : "i.st20#gmail.com", "tos.address" : "ron#gmail.com"}).
I have got records in my collection as shown below
{
"_id" : ObjectId("53722c39e4b04a53021cf3c6"),
"symbol" : "AIA",
"tbq" : 1356,
"tsq" : 0,
"tquan" : 6831336,
"tvol" : 17331.78,
"bquantity" : 1356,
"squantity" : 0
}
{
"_id" : ObjectId("53722c38e4b04a53021cf3c1"),
"symbol" : "SAA",
"tbq" : 0,
"tsq" : 9200,
"tquan" : 6036143,
"tvol" : 50207.43,
"bquantity" : 0,
"squantity" : 9200
}
I am displaying the results in the ascending order of bquantity, and also at the same time I want to display only certain columns in the result (symbol, bquantity, squantity) and ignore the rest.
I tried with the below query, but still its displaying all the fields .
Please tell me how can i eliminate those fields from the result ?
db.stock.find().sort(
{"bquantity":-1},
{
symbol: 1,
bquantity: 1,
squantity:1 ,
_id:0,
tbq:0,
tsq:0,
tquan:0,
tvol:0
}
)
The field filter is a parameter to the find function, not to the sort function, i.e.:
db.stock.find({}, { _id:0,tbq:0, tsq:0,tquan:0,tvol:0}).sort({"bquantity":-1})
The empty hash used as first parameter to find is required as an 'empty query' which matches all documents in stock.