MongoDB find document in collection based on 2 IDs PHP - php

I was wondering how to find document in collection based on UIDa and UIDb which could be also vice versa.
So for example I want to get all documents where
value UIDa = inputA and UIDb = inputB
or value UIDa = inputB and UIDb = inputA
{
"_id" : ObjectId("5572f65dc89782b9398b4fc0"),
"sender" : "senderID",
"recipient" : "recipientID",
"message" : "messagehere",
"time" : "1433092679",
"state" : "1"
}
I have this query but it doesn't work because some of the documents are missing.
$query = $db->collection->find(array(
'$or' => array(
array(
'recipient' => $uid,
'sender' => $fuid,
'state' => '1',
'time' => array(
'$lt' => $last_update
)
),
array(
'recipient' => $fuid,
'sender' => $uid,
'state' => '1',
'time' => array(
'$lt' => $last_update
)
)
)
)
)->limit(20);
thanks in advance.

Well I think what you are looking for the $in operator.
$query = $db->collection->find(array(
'recepient' => array('$in' => array($uid, $fuid)),
'sender' => array('$in' => array($uid, $fuid)),
'state' => '1',
'time' => array('$lt' => $last_update)
)
)->limit(20);

Related

How to convert mongoDB query into PHP?

My query is working in mongodb and produce output Properly. But same query i tried to run using php then it gives following error
Array
(
[ok] => 0
[errmsg] => A pipeline stage specification object must contain exactly one field.
[code] => 16435
)
Following is my MongoDB Query
db.sample_coll.aggregate(
{
$unwind: {
path:"$KeyValues",
includeArrayIndex:"arrayIndex",
preserveNullAndEmptyArrays:true
}
},
{
$project: {
timestamp:{
"$add":["$EventTS",{"$multiply":[60000,"$arrayIndex"]}]
} ,
"InputVolt":"$KeyValues.InputVolt",
arrayIndex:1
}
},
{
$match: {
$and: [
{InputVolt: {$ne: null}}
]
}
}
);
The Above query is converted in php
$pipeline = array(
array('$unwind' =>
array( 'path' => '$KeyValues'),
array( 'includeArrayIndex' => 'arrayIndex' ),
array( 'preserveNullAndEmptyArrays' => 'true' )
),
array(
'$project' => array(
'timestamp' => array(
'$add' => array(
'$EventTS',
array('$multiply' => array( 60000, '$arrayIndex' ))
)
),
array( 'InputVolt' => array( '$KeyValues', 'InputVolt' ) ) ,
array('arrayIndex' => 1)
)
),
array(
'$match' => array(
'$and' => array(
array('InputVolt' => array('$ne' => null )),
)
)
)
);
$result = $collection->aggregate($pipeline);
Kindly help to resolve this issue.
Thanks in advance
Your pipeline in PHP:
$pipeline = array(
array('$unwind' => array(
'path' => '$KeyValues',
'includeArrayIndex' => 'arrayIndex',
'preserveNullAndEmptyArrays' => true
)),
array('$project' => array(
'timestamp' => array(
'$add' => array('$EventTS', array('$multiply' => array(60000, '$arrayIndex')))
),
'InputVolt' => '$KeyValues.InputVolt',
'arrayIndex' => 1
)),
array('$match' => array(
'InputVolt' => array('$ne' => null)
))
);

Mysql Between query in elasticsearch

Currently I am Working on Elastic Search For My Current Project. I need MySql BETWEEN in my elastic. I searched So Many Examples But Not Get Any Solution I need to sort using no. of employees by Company. I already Wrote Some Filters Its Working Fine. But I Need Range Filter. and my code as follows
$query = array("from" => $start,
"size" => $recordslimit,
"sort" => array(array('id' => 'desc')),
"query" => array(
"filtered" => array(
"query" => array("match_all" => array()),
"query" => (!empty($keyword)) ? array("match" => array('_all' => $keyword)) : null,
"filter" => array(
"bool" => array(
'should' => array(
array("match" => array('location' => $headerLocation))
),
'must' => array(
($type == '1') ? array('term' => array('user_sub_type' => '1')) : null,
array('term' => array('user_type' => 'v')),
array('term' => array('status' => 'a'))
),
'must_not' => array(
array('term' => array('subscription_type' => 'n'))
))
)
))
);
}
}
$data = $this->elasticsearch->advancedquery("users",json_encode($query));
this is current query.. and also i add in filter
"range" => array(
'num_of_employees' => array(
'gt' => 100,
'lte' => 1000
)
)
please helpout with this situation. Thanks in Advanced.

how to write cakephp and and or query

$tmp_sales = $this->tmp_sale->find('all', array(
'conditions' => array(
array(
'no' => $no,
'barcode' => $barcode,'employee' => $emp, 'store' => $store_name
)
)
));
This is my present query.
select * from tmp_sale where no = '$no' and
'employee' ='$emp' and store = '$store_name' and ( barcode='$barcode' or name='$barcode')
I want to make change like above . how to write query like this in cakephp
'conditions' => array(
'no' => $no,
'employee' => $emp,
'store '=> $store_name,
'OR' => array(
array('barcode' => $barcode),
array('name' => $barcode)
)
)
Cakephp Model name should be Singular,tmp_sale Model name should be replaced by TmpSale name. The query of cakephp shown in below.
You should try this
$tmp_sales = $this->TmpSale->find('all', array(
'conditions' => array( 'no' => $no,
'employee' => $emp,
'store '=>$store_name,
'or'=>array('barcode'=>$barcode,
'name'=>$barcode)
)
));
Thanks

Unique Merge multiple multidimensional array

I have some difficulties to merge many multidimensional array in php. I tried to do it by many way, but each time, I don't get the result wanted. I tried with array_merge(array_unique,...) and in different post I found a way with array_map, but I don't understand everything...
I can have many multi array like below:
array(
(int) 0 => array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'AM'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
),
(int) 1 => array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'PM'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
)
)
And I would like to get this kind of array :
array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'Full day'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
)
Do you have some advices to give me to get this result ?
Thank you very much !
I don't know if the best solution but it seems to work like this, and fastly :
foreach ($users as $k=>$v){
//$r[$k] = array_merge($v,$users[$k]);
//$unique[] = array_map("unserialize", array_unique(array_map("serialize", $users[$k])));
$s[$k] = array(
'username' => $v['User']['username'],
'team' => $v['Team']['name'],
'period' => $v['Calendar']['period']
);
if ($k > 0) {
if (in_array($v['User']['username'],$s[$k])) {
unset($s[$k-1]);
$s[$k] = array(
'username' => $v['User']['username'],
'team' => $v['Team']['name'],
'period' => "FD"
);
}
}
}
Do you have another idea or this one is enough good ?
thank you !

Lithium framework multiple 'or' statements in a query (using mongoDB)

I have started work with lithium framework + mongoDB recently. I want to do a really simple query which contains multiple or statements.
I have articles in the DB with publish_up and publish_down fields. I want to fetch only those records/documents which pulbis_down field is highert than now OR null AND publish_up field lower than now OR null.
$items = $article::find('all', array(
'conditions' => array(
'$or' => array(
'$gt' => array('publish_down', $mongoDateNow),
'publish_down' => $mongDateNull
),
'$or' => array(
'$lt' => array('publish_up', $mongoDateNow),
'publish_up' => $mongDateNull
),
)
));
Of course this snippet is wrong hence the second or statement overwrites the first one (because the same array key).
I tried to wrap them into an individual array but gives error.
Any idea?
This query will fetch articles with (publish_down > now OR publish_down = null) AND (publish_up < now OR publish_up = null)
$items = Articles::find('all', array(
'conditions' => array(
'$or' => array(
array('publish_down' => array('$gt' => $mongoDateNow)),
array('publish_down' => null)
),
'$or' => array(
array('publish_up' => array('$lt' => $mongoDateNow)),
array('publish_up' => null)
),
)
));
I don't know about lithium but in PHP you can use multiple $OR as below
$items = $article::find('all', array(
'conditions' => array(
'$or' => array(
array(
'$gt' => array('publish_down', $mongoDateNow),
'publish_down' => $mongDateNull
),
array(
'$lt' => array('publish_up', $mongoDateNow),
'publish_up' => $mongDateNull
)
),
)
));
I think the correct answer is:
'$and' => array(
array(
'$or'=>array(
array('publish_up' => null),
array('publish_up' => array('$lt' => $mongoDateNow))
)
),
array(
'$or'=>array(
array('publish_down' => null),
array('publish_down' => array('$gt' => $mongoDateNow))
)
)
)

Categories