I’m trying to fetch some data from appointment table. But when execute the following code, data not fetching. In my mongo I have data with matching criteria. Any help would be appreciated advance.
Can anyone check the condition which I’m giving right or wrong.
var timePeriod = {
to: 1512412200000,
from: 1511807400000
}
var List= appointment.find({$and:[{‘appointmentDate’:
{$gte:Number(timePeriod.from)}},{‘appointmentDate’:
{lte:Number(timePeriod.to)}},
{‘url’:{$eq:‘boaseenterprises.ezvisitor.com’}}]}).fetch();
mongo collection data
{
"_id" : “SpwCxi4CsxDFanvrf”,
“appointmentDate” : 1511952239000.0,
“appointmentTime” : 1511952239000.0,
“appointmentEndDate” : 1511955839000.0,
“appointmentEndTime” : 1511955839000.0,
“purpose” : “t”,
“hostName” : “boase”,
“status” : “pending”,
“hostId” : “Tct2pRanpHW5pju6A”,
“url” : “boaseenterprises.ezvisitor.com”,
“visitorsList” : [
{
“id” : “9i8u9tFwyuFi7xjmE”,
“name” : “test1”,
“signIn” : 0,
“signOut” : 0
}
]
}
Looking at your query, you are trying to find all those appointments which is between timePeriod.from and timePeriod.to and where url is boaseenterprises.ezvisitor.com
appointment.find({
appointmentDate: {$gte:timeperiod.from},
appointmentDate: {$lt:timePeriod.to},
url : "boaseenterprises.ezvisitor.com"
}).fetch();
Related
I have mongo documents like the following:
{
"_id" : ObjectId("591e36eb2cdce09936d8e511"),
"bday" : null,
"studentId" : "000004"
}
I want to make it :
{
"_id" : ObjectId("591e36eb2cdce09936d8e511"),
"bday" : null,
"studentId" : {
"id" : "000004"
}
}
I have tried the following:
db.persons.update({"studentId": "000004"},{$set : {"studentId.id": "000004"}},false,true)
But it is giving an error:
LEFT_SUBFIELD only supports Object: studentId not: 2
Can anyone help?
Thanks,
Sumit.
In your case you see an error because you are trying to take "studentId" which is string and add a property inside it. To fix it you should set entire new object like this
db.persons.update({"studentId":"000004"},{$set:{"studentId":{"id":"000004"}}})
Another option, if, for example you need to change structure for entire collection, is to just iterate over each element and update them. Like this:
db.persons.find({}).forEach(function (item) {
if (item.studentId != null){
item.studentId = {id:item.studentId};
db.persons.save(item);
}
});
I hope this will help
db.collection.update({_id:ObjectId("591e36eb2cdce09936d8e511")},{$set:{"studentId":{"id":"000004"}}})
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 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.
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");
}