i want to find records which are between "startdate" & "enddate". i get this record for date like "2013-05-22", "2013-05-20","2013-05-10","2013-05-05" etc.
my fields in collection are as below :
array (
'_id' => new MongoId("518221f16541b9980d000002"),
'userId' => '2d40981c83a37e758ced05dc325e40fa',
'kpiId' => 'f516ed1c59e6b310c47cdfc06abb17aa',
'dateOpt' => 'customize',
'startDate' => '2013-05-02',
'endDate' => '2013-05-22',
'personalTarget' => '',
'assignedTarget' => '100000.00',
'singleTargetAmt' => 6666.6666666667,
'createdOn' => '2013-05-02 13:51:05',
'updatedOn' => '2013-05-02 13:51:05',
'distributionMethod' => 'manual',
)
and my query is :
array(
"kpiId" => 'f516ed1c59e6b310c47cdfc06abb17aa',
"userId" => '2d40981c83a37e758ced05dc325e40fa',
"startDate" => array
(
'$gte' => '2013-05-03'
),
"endDate" => array
(
'$lte' => '2013-05-03'
)
)
it gives no records. my query is wrong ?
Try something like this
Query query = new Query(Criteria.elemMatch(
Criteria.where("startDate").lte(date)
.and("endDate").gte(date)
);
Related
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);
Ok I am not sure why this is not working I know the field is there because it has sub arrays in this mydetails field.
function firsttime($uid){
$collection = static::db()->members;
var_dump($collection->findOne(array("_id"=> new MongoId($uid), array("mydetails"=> array('$exists' => true)))));
}
all it returns is NULL
is there a better way to find if there is or is not a field
in this example I want to see if the field mydetails exist?
It would be nice if I could either have a true or false return.
an example data
array (
'_id' => new MongoId("53b9ea3ae7fda8863c8b4568"),
'mydetails' =>
array (
'name' =>
array (
'first' => 'Russell',
'last' => 'Harrower',
),
'email' => 'hidden#ipet.xyz',
'birthday' =>
array (
'day' => '02',
'month' => '02',
'year' => '1988',
),
)
)
You got an array( too much in there. Try this:
$collection->findOne(array("_id"=> new MongoId($uid), "mydetails"=> array('$exists' => true)));
I am trying to return a collection of messages grouped by in_reply_to field, I have this code:
$result = $this->db->Message->aggregate(
array(
array(
'$project' => array('message' => 1, 'in_reply_to'=> 1, 'to_user' => 1, 'from_user' => 1)
),
array(
'$group' => array('_id' => '$in_reply_to'),
),
)
);
print_r($result);exit;
the result is:
Array (
[result] => Array (
[0] => Array (
[_id] => MongoId Object (
[$id] => 53a03d43b3f7e236470041a8
)
)
[1] => Array (
[_id] => MongoId Object (
[$id] => 53a03cbdb3f7e2e8350041bb
)
)
)
[ok] => 1
)
Ideally I'd like the entire Message object, but I did think that $project would be used to specify returns fields, even so, I dont get the fields I'm specifying.
Any help is greatly appreciated
In order to get all the messages in the thread you basically want to $push
$result = $this->db->Message->aggregate(
array(
array(
'$group' => array(
'_id' => '$in_reply_to',
'messages' => array(
'$push' => array(
'_id' => '$_id',
'message' => '$message',
'to_user' => '$to_user',
'from_user' =>'$from_user'
)
)
)
)
)
);
MongoDB 2.6 you have the $$ROOT variable that shortens this:
$result = $this->db->Message->aggregate(
array(
array(
'$group' => array(
'_id' => '$in_reply_to',
'messages' => array(
'$push' => '$$ROOT'
)
)
)
)
);
So that puts all of the related messages inside the "messages" array tied to that key.
Just as side note, while you can do this you may as well just sort the results by your "in_reply_to" field and process them that way looking for changes in the value to indicate a new thread.
Sorting with a find would be the fastest way to process, even if it does not conveniently put everything right under the one key.
If you want to get additional fields beside _id field, when using $group operator, you need to include them using some of the available accummulators like $first or $last. You can see the full list on the MongoDB $group documentation page.
The query will look like this:
$result = $this->db->Message->aggregate(
array(
array(
'$project' => array(
'message' => 1,
'in_reply_to'=> 1,
'to_user' => 1,
'from_user' => 1
)
),
array(
'$group' => array(
'_id' => '$in_reply_to',
'message' => array('$first' => '$message'),
'to_user' => ('$first' => '$to_user'),
'from_user' => ('$first' => '$from_user')
),
),
)
);
If the message, to_user and from_user values are same in all documents using $last instead of $first $last will produce the same results.
I have a collection in mongoDb. it is similar to the following.
array(
'_id' => new MongoId("50b35d1217ce10ac1000000f")
'Education' =>
array (
'content' =>
array (
'0' =>
array (
'Organization' => 'SUST',
'Degree' => 'BSC',
'Department' => '',
'Location' => 'Dhaka',
'Session' => '2 Years',
),
'1' =>
array (
'Organization' => 'DU',
'Degree' => 'BSC',
'Department' => '',
'Location' => 'Dhaka',
'Session' => '2 Years',
)
),
'sharing' => 'public',
),
)
I want to delete Education.content.1 from the collection.
So i used the
update(array('_id' => new MongoId('50b35d1217ce10ac1000000f')), array('$unset' => array('Education.content.1' => 1)));
As a result Education.content.1 becomes null.
But I want Education.content.1 to be deleted not to be null.
Please help me if any one knows the solution. Thanks in advance.
Use $pull after $unset:
update(array('_id' => new MongoId('50b35d1217ce10ac1000000f')),
array('$unset' => array('Education.content.1' => 1)));
update(array('_id' => new MongoId('50b35d1217ce10ac1000000f')),
array('$pull' => array('Education.content' => null)));
I have a mongodb object as follows:
array (
'_id' => new MongoId("4cc97fb0247ae8747ec5fefb"),
'posts' =>
array (
0 =>
array (
'comment' => 'Eamorr',
'fromUname' => 'Eamorr',
'time' => 1288273840,
'UTC' => '2010-10-28T14:50:40+01:00',
'ip' => '127.0.0.1',
'id' => '123lasdfiqwoei28asdf',
),
1 =>
array (
'comment' => 'Hello',
'fromUname' => 'Eamorr',
'time' => 1288277023,
'UTC' => '2010-10-28T15:43:43+01:00',
'ip' => '127.0.0.1',
'id' => 'qopqwier982389qwfa',
),
2 =>
array (
'comment' => 'Hello',
'fromUname' => 'Anonymous',
'time' => 1288283506,
'UTC' => '2010-10-28T17:31:46+01:00',
'ip' => '127.0.0.1',
'id' => 'ioqwoeias892398wrf',
),
/////
//Want to remove element 3:
/////
3 =>
array (
'comment' => 'asdfasadf',
'fromUname' => 'Anonymous',
'time' => 1288283864,
'UTC' => '2010-10-28T17:37:44+01:00',
'ip' => '127.0.0.1',
'id' => 'wwwwwiasdfn234oiasf',
),
4 =>
array (
'comment' => 'asdfasdfasdf',
'fromUname' => 'Anonymous',
'time' => 1288284076,
'UTC' => '2010-10-28T17:41:16+01:00',
'ip' => '127.0.0.1',
'id' => '290qwefoiqweproiqwerpq',
),
5 =>
array (
'comment' => 'ASDF',
'fromUname' => 'Eamorr',
'time' => 1288284331,
'UTC' => '2010-10-28T17:45:31+01:00',
'ip' => '127.0.0.1',
'id' => 'eioqw8923892hasdf',
),
6 =>
array (
'comment' => 'ASDF2',
'fromUname' => 'Eamorr',
'time' => 1288284370,
'UTC' => '2010-10-28T17:46:10+01:00',
'ip' => '127.0.0.1',
'id' => '23oaiofsaij234',
),
),
'uname' => 'Eamorr',
)
Now, I am writing some PHP code to remove posts[x]. I'm trying to use $pull to remove the array element.
I have an 'id' 'wwwwwiasdfn234oiasf' (array element 3) and I want to remove this entire array element, leaving just 6 elements in the 'posts' array.
I've tried googling and looking up the documentation to no avail... I still can't get the hang of the mongodb syntax.
I'm doing all this in PHP, but any language will do I should be able to do the translation.
Many thanks in advance,
Solution (in PHP):
$uname=whatever
$id=whatever
$mongo=new Mongo();
$walls=$mongo->people->walls;
$walls->update(array('uname'=>$uname),array('$pull'=>array('posts'=>array('id'=>$id))));
Here's how to do it using the MongoDB shell. You should be able to translate it into PHP.
A pull operation consists of the $pull modifier, a field selector and a value expression.
{ $pull: { fieldSelector: valueExpression } }
In your case the field selector is posts, since that's the array you want to update. The value expression, in plain English, is
where the id of the post equals "wwwwwiasdfn234oiasf"
This translates to { id: "wwwwwiasdfn234oiasf" }. If we combine all of this, you'll get the following $pull statement, which will remove the desired item from the array:
{ $pull: { posts: { id: "wwwwwiasdfn234oiasf" } } }