Below is my one of sample document in mongodb
{
"_id" : ObjectId("59c5fcedc817c3da1b8b456a"),
"zoneid" : "20",
"accountid" : "34",
"sitename" : "aaa",
"requestid" : "2efa1052-7255-41cd-9d39-8628dac5ed3e",
"winbid" : NumberLong("1"),
"bidder" : "innity",
"cpm" : NumberLong("1"),
"responsetime" : NumberLong("207"),
"slotid" : "20x300x250x34x9206",
"viewerid" : "_87cd184234b181d1c601144adc0c3621",
"uniquecount" : NumberLong("0"),
"datetime" : "2017-09-23 06:19:25"
}
and below is my code to fetch data in mongodb in php,
$ops = array(
array(
'$group' => array(
"_id" => array(
"slotid" => '$slotid',
"bidder" => '$bidder',
"viewerid" => '$viewerid',
"datetime" => '$datetime'
) ,
"total" => array(
'$sum' => 1
) ,
"sitename" => array(
'$addToSet' => '$sitename'
) ,
"aid" => array(
'$addToSet' => '$accountid'
)
)
) ,
array(
'$match' => array(
"datetime" => array(
'$gt' => '2017-09-23 06:00:00'
)
)
)
);
$reqbids = $db->requestbids->aggregate($ops);
Its return empty result but if I remove either '$group' array or '$match' array means result will shown,but both will used means empty result shown,like below query its shown result
$ops = array(
array(
'$group' => array(
"_id" => array(
"slotid" => '$slotid',
"bidder" => '$bidder',
"viewerid" => '$viewerid',
"datetime" => '$datetime'
) ,
"total" => array(
'$sum' => 1
) ,
"sitename" => array(
'$addToSet' => '$sitename'
) ,
"aid" => array(
'$addToSet' => '$accountid'
)
)
)
);
$reqbids = $db->requestbids->aggregate($ops);
In above query I removed '$match' array , then its shown result.whats the issue in the code,Anyone help to solve my problem,Any help appreciated...
Related
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)
))
);
ElasticSearch returns me [_na] query malformed, no field after start_object error when trying to look up entries using the following query. The field localtime is a new field of documents and exist in every document.
php code,
$qryurl = '<myurl>:<myport>/index/_search?pretty';
$data = array(
"query" => array(
"bool" => array(
"must" => array(
"range" => array(
"localtime" => array(
"from" => "2016-06-15T17:43:04.923Z",
"to" => "2016-06-17T17:43:04.923Z",
"include_lower" => "true",
"include_upper" => "true"
)
),
"term" => array(
"query" => "1.2.3.4",
"fields" => array("ip")
),
"query_string" => array(
"query" => "*up*",
"default_field" => array("_all")
)
)
)
);
Why does this error appear?
anyhelp will be appreciated ! thanks!
Your bool/must clause must be a pure array not an associative array:
$qryurl = '<myurl>:<myport>/index/_search?pretty';
$data = array(
"query" => array (
"bool" => array (
"must" => array(
array(
"range" => array (
"localtime" => array (
"from" =>"2016-06-15T17:43:04.923Z",
"to" => "2016-06-17T17:43:04.923Z",
"include_lower" => "true",
"include_upper" => "true"
)
)
),
array(
"term" => array(
"ip" => "1.2.3.4"
)
),
array(
"query_string" => array(
"query" => "*up*",
"default_field" => "_all"
)
)
)
)
)
);
I would like to make a request with two $ group.
Here you can see the structure of my data :
{
"_id" : ObjectId("573495af4e998fec800041a7"),
"uniqid" : "not573495aeda725",
"status" : "waiting,
"date" : ISODate("2016-05-12T14:39:42.000+0000"),
"id_transaction" : null,
"hash_file" : null,
"user" : "Michel",
"desc" : "undefined",
"pharmacy" : "p56cdc980ba57f"
},
{
"_id" : ObjectId("573495af4e998fec800041a7"),
"uniqid" : "not573495aeda725",
"status" : "waiting,
"date" : ISODate("2016-05-12T14:39:42.000+0000"),
"id_transaction" : null,
"hash_file" : null,
"user" : "Julien",
"desc" : "undefined",
"pharmacy" : "p72gdf210xs68t"
}
Here is where i am in my aggregate from now :
$pipeline = array(
array(
'$project' => array(
'year' => array('$year' => array('$add' => array('$date',$offset))),
'month' => array('$month' => array('$add' => array('$date',$offset))),
'day' => array('$dayOfMonth' => array('$add' => array('$date',$offset))),
'hour' => array('$hour' => array('$add' => array('$date',$offset))),
"uniqid" => 1,
"status" => 1,
"date" => 1,
"pharmacy" => 1
)
),
array(
'$match' => array(
'$and' => array(
array(
"status" => array('$ne' => null)
),
array(
"status" => array('$ne' => "error")
),
array(
"date" => array('$lte' => new MongoDate(time()))
),
array(
"date" => array('$gt' => new MongoDate(strtotime($sixMonthAgo->format('Y-m-d'))))
)
)
)
),
array(
'$group' => array(
'_id' => array(
"month" => array('$month' => '$date'),
"year" => array('$year' => '$date'),
),
'count' => array('$sum' => 1)
)
),
array(
'$group' => array(
'_id' => array(
),
'count' => array('$sum' => 1)
)
)
);
I would like to have the number on lines where status = "waiting" grouping my request by "year/month" and by "pharmacy".
Thank you so much !
array(
'$group' => array(
'_id' => array(
"month" => array('$month' => '$date'),
"year" => array('$year' => '$date'),
"status"
),
'count' => array('$sum' => 1)
)
),
then instead of grouping - use match for _id.status ="waiting"
You do not need any $project ... Mongo Query should be -
[
{
"$match": {
"status": "waiting",
"date": {
"$gt": {
"$date": "DATE_SIX_MONTH_AGO"
},
"$lt": {
"$date": "CURRENT_DATE"
}
}
}
},
{
"$group": {
"_id": {
"month": {
"$month": "$date"
},
"year": {
"$year": "$date"
},
"pharmacy": "$pharmacy"
},
"count": {
"$sum": 1
}
}
}
]
In PHP it should be -
$pipeline = array(
array(
'$match' => array(
"status" => "waiting",
"date" => array(
'$lte' => new MongoDate(time()),
'$gt' => new MongoDate(strtotime($sixMonthAgo->format('Y-m-d')))
)
)
),
array(
'$group' => array(
'_id' => array(
"month" => array('$month' => '$date'),
"year" => array('$year' => '$date'),
"pharmacy" => '$pharmacy'
),
'count' => array('$sum' => 1)
)
)
);
I did not test in PHP, check and correct the syntax errors if any.
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);
i am failing to convert the following mongoDB console command:
db.customers.aggregate( [
{ $group : {
_id: {
year : { $year: "$since" },
month : { $month: "$since" }
},
count: { $sum: 1 }
}
}]
);
which works into php
$customers->aggregate(array(
'$group' => array(
'_id' => array( 'year' => array('$year' => '$since'),
'month' => array('$month' => '$since')
)
),
array(
'count' => array( '$sum' => 1 )
),
)
);
which returns exception: A pipeline stage specification object must contain exactly one field.
also already tried '"$since"' with no luck
The count field must be a part of the group.
$customers->aggregate(array(
'$group' => array(
'_id' => array( 'year' => array('$year' => '$since'),
'month' => array('$month' => '$since')
),
'count' => array( '$sum' => 1 )
)
));