The data in hotel table is stored like
{
_id: ....,
HotelName: ...,
MasterKeyDetails:[
{
MasterKeyId: 36K1,
...
},
{
MasterKeyId: 36J1,
...
}
]
}
I have written below query
$cursor = $this->collection->aggregate(array(
array(
'$match' => array(
"_id" => new MongoDB\BSON\ObjectID($this->id)
)
),
array(
'$project' => array(
'MasterKeyDetails' => array(
'$filter' => array(
'input' => '$MasterKeyDetails',
'as' => 'room',
'cond' => array(
'$eq' => array('$$room.MasterKeyId', $this->MasterKeyId)
)
)
),
)
)
)
)->toArray();
It is searching fine. It searches when $this->MasterKeyId contains 36K1, but does not search when $this->MasterKeyId contains 36k1. I want it should fetch despite of case sensitive data...
Please help!!!
You can use $toUpper to normalise the data:
$cursor = $this->collection->aggregate(array(
array(
'$match' => array(
"_id" => new MongoDB\BSON\ObjectID($this->id)
)
),
array(
'$project' => array(
'MasterKeyDetails' => array(
'$filter' => array(
'input' => '$MasterKeyDetails',
'as' => 'room',
'cond' => array(
'$eq' => array(array('$toUpper' =>'$$room.MasterKeyId'), $this->MasterKeyId)
)
)
),
)
)
)
)->toArray();
In the same way you make sure the data you have to supply for comparison is also in the same case. There is also $toLower if you prefer that way.
As yet there is no other "case insensitive match" function you can use as part of an aggregation pipeline, so "normalizing" is the current approach.
Related
I have following query where I am using same array index for three fields. But query response is incorrect because of using same arra index. Using query as follows
$pipeline = array(
array(
'$project' => array(
'YPhaseVoltage' => 1,
'RPhaseVoltage' => 1,
'EventTS' => 1
)
),
array(
'$unwind' => array(
'path' => '$RPhaseVoltage',
'includeArrayIndex' => "arrayIndex",
'preserveNullAndEmptyArrays' => true
)
),
array(
'$unwind' => array(
'path' => '$YPhaseVoltage',
'includeArrayIndex' => "arrayIndex",
'preserveNullAndEmptyArrays' => true
)
),
array('$match' => array(
'$and' => array(
array("RPhaseVoltage" => array('$ne' => null)),
array("YPhaseVoltage" => array('$ne' => null))
)
),
),
array(
'$project' => array(
'_id' => 0,
'RPhaseVoltage' => 1,
'YPhaseVoltage' => 1,
'RYBTimestamp' => array(
'$add' => array(
array('$subtract' => array('$EventTS', $default_date)),
array('$multiply' => array( 60000, '$arrayIndex' ))
)
)
)
),
array(
'$project' => array(
'rvoltage_data' => array('$RYBTimestamp', '$RPhaseVoltage'),
'yvoltage_data' => array('$RYBTimestamp', '$YPhaseVoltage'),
'RYBTimestamp' => 1
)
),
array(
'$sort' => array('RYBTimestamp' => 1),
),
array(
'$limit' => 50
)
);
$result = $collection->aggregate($pipeline);
I want to use individual array index for each filed. Which gives me expected response. Please suggest me how to use multiple array index in mongodb aggregate?
How can i sum of quantity using mongodb query in yii2?
In my database there are entries like below:
{
'_id': ObjectId("560e36f91e4acbf113d14f1f"),
'user_id': '55ded7c31e4acbea3c8b4567',
'money': '1.99',
'quantity': '1',
'purchased_date_time': '2015-10-02 07:49:13'
}
{
'_id': ObjectId("560e36f71e4acbf113d14f1e"),
'user_id': '55ded7c31e4acbea3c8b4567',
'money': '1.99',
'quantity': '1',
'purchased_date_time': '2015-10-02 07:49:11'
}
{
'_id': ObjectId("560d17381e4acbf013d14f1e"),
'user_id': '55ded7c31e4acbea3c8b4567',
'money': '4.99',
'quantity': '3',
'purchased_date_time': '2015-10-01 11:21:28'
}
I want to sum of quantity where user_id = 55ded7c31e4acbea3c8b4567.
What i tried is:
$collection = Yii::$app->mongodb->getCollection('purchase');
$result = $collection->aggregate(
array( '$match' => array( 'user_id' => '55ded7c31e4acbea3c8b4567') ),
array( '$group' => array( '_id' => NULL,
'quantity' => array( '$sum' => (int)'$quantity' )
))
);
But always i am getting this result:
<pre>Array
(
[0] => Array
(
[_id] =>
[quantity] => 0
)
)
</pre>
Can anyone help?
Remove int then after check......
I solved my issue after remove it
$collection = Yii::$app->mongodb->getCollection('purchase');
$result = $collection->aggregate(
array( '$match' => array( 'user_id' => '55ded7c31e4acbea3c8b4567') ),
array( '$group' => array( '_id' => NULL,
'quantity' => array( '$sum' => '$quantity' )
))
);
Is it possible to get the files from a gridFS mongo database with a aggregate query?
I have the following query:
$ops = array(
array(
'$match' => array(
"_id" => array('$lt' => new MongoId($id)),
"data.VisionId" => (string)$visionId,
"data.GroepNummer" => (string)$groepnummer,
"data.TypeFile" => ucfirst($typefile),
'data.Datum' => array('$gt' => $new_date, '$lte' => $datum),
)
),
array(
'$sort' => array(
"uploadDate" => -1,
)
),
array(
'$limit' => 1000
),
array(
'$group' => array(
"_id" => array("Datum" => '$data.Datum',),
"FirstId" => array('$first' => '$_id'),
),
),
);
$result = $gridFS->aggregate($ops);
With a foreach I get the result from this aggregate.
But how do I get the data from the file.
(with a normal query i do it like $obj2->getBytes();)
I have one collection which contain documents with following structure:
array (
'_id' => new MongoId("54369e2904f7055598463483"),
'user' => '123',
'model' =>
array (
'test' =>
array (
'0' => 'test1',
'1' => 'test2',
'2' => 'test3',
),
),
)
And if I do the next query in Rock Mongo I got the results.
array(
'user' => array(
'$in' => array(
'0' => '123'
)
)
)
But if I try to fetch documents which have for example key => pair value '0' => 'test1' & '1' => 'test2' I don't get any data with the next query:
array(
'model' => array(
'test' => array(
'$in' => array(
'0' => 'test1',
'1' => 'test2',
)
)
)
)
Where I'm making mistake?
Thanks in advance
PHP's array/hash mixup makes your example a little hard to read and reason about. But here's what your model looks like in Mongo's native syntax (a language which makes a distinction between lists and maps):
{
"_id": ObjectId(...),
"user": 123,
"model": {"test": ["test1", "test2", "test3"]}
}
So you want to query on model.test (nested document). Using Mongo's native syntax, you'd do the following:
collection.find({"model.test": {"$in": ["test1", "test2"]})
See http://docs.mongodb.org/manual/reference/method/db.collection.find/ for more info.
I have this aggregation:
$out = $db->stats->aggregate (
array('$match' => $where),
,array( '$group' =>
array( "_id" => '$traffic.source',
'count'=> array('$sum' => 1)
)
)
,array( '$project' =>
array( "_id" => 0,
'type' => '$_id',
'count' => '$count'
)
)
);
which returns an array with:
[{type:sourceA, count:2},{type:sourceB, count:6}...]
Is it possible to make it return:
[sourceA:2, sourceB:6,....] without looping the array afterwords?