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)));
Related
I've been scratching my head and failing miserably at coming up with a solution to my array structuring issue. I'm not sure exactly what part would be better to try and fix, the data being returned from SQL or the PHP array after the fact.
My SQL data is returned like this:
$i = 0;
while ( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) ) {
$colData[$i] = array(
'name' => $row['FULLNAME'],
'invoice' => $row['CUST_InvoiceNumber_020911544'],
array(
'service' => $row['CUST_Service_052400634'],
'date' => date_normalizer($row['CUST_ServiceDate_064616924']),
'service_amount' => $row['CUST_ServiceAmount_054855553'],
),
'do_all_for' => $row['CUST_DoAllFor_021206685'],
'memo' => $row['CUST_Memo_021614200'],
'paymenttype' => $row['CUST_PAYMENTTYPE_123838203'],
'deposit' => $row['CUST_DEPOSIT_124139703'],
'datepaid' => date_normalizer($row['CUST_DATEPAID_124941578']),
);
$i++;
}
And the resultant array has this structure:
array (
0 =>
array (
'name' => 'ABRAHAM PRETORIS',
'invoice' => '63954',
0 =>
array (
'service' => 'Tree Work',
'date' => '2015-01-22',
'service_amount' => '1305.00',
),
'do_all_for' => '4924.68',
'memo' => 'CHECK #947 $2400',
'paymenttype' => 'VISA',
'deposit' => '4429.48',
'datepaid' => '2015-02-09',
),
1 =>
array (
'name' => 'ABRAHAM PRETORIS',
'invoice' => '63954',
0 =>
array (
'service' => 'DRF',
'date' => '2015-01-22',
'service_amount' => '740.00',
),
'do_all_for' => '4924.68',
'memo' => 'CHECK #947 $2400',
'paymenttype' => 'VISA',
'deposit' => '4429.48',
'datepaid' => '2015-02-09',
),
2 =>
array (
'name' => 'ABRAHAM PRETORIS',
'invoice' => '63954',
0 =>
array (
'service' => 'Stumps',
'date' => '2015-01-26',
'service_amount' => '360.00',
),
'do_all_for' => '4924.68',
'memo' => 'CHECK #947 $2400',
'paymenttype' => 'VISA',
'deposit' => '4429.48',
'datepaid' => '2015-02-09',
),
Notice that I'm getting a new subarray for the same person because the sub-subarray (service, date & service_amount) has multiple values.
What I'm trying to accomplish is condensing the array so that I only have one array for "ABRAHAM PRETORIS" etc, but all of the different services listed as a sub array. I would like it to look like this:
array (
0 =>
array (
'name' => 'ABRAHAM PRETORIS',
'invoice' => '63954',
0 =>
array (
'service' => 'Tree Work',
'date' => '2015-01-22',
'service_amount' => '1305.00',
),
1 =>
array (
'service' => 'DRF',
'date' => '2015-01-22',
'service_amount' => '740.00',
),
2 =>
array (
'service' => 'STUMPS',
'date' => '2015-01-26',
'service_amount' => '360.00',
),
'do_all_for' => '4924.68',
'memo' => 'CHECK #947 $2400',
'paymenttype' => 'VISA',
'deposit' => '4429.48',
'datepaid' => '2015-02-09',
),
I've looked at tons of examples of nested foreach statements and php array functions but I just can't wrap my head around how to loop through and add the additional services to the array then proceed when it's a row with a different name and/or invoice number.
Thanks in advance for the help!!
First, make sure your SQL query has an order by name, invoice. That will ensure all the records you want to group are sequential.
Then you have to create a loop with some additional inner logic:
// Creates an array to hold the final array.
$result = array();
// This var will keep track of name changes.
$current_name = '';
while ( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) )
{
// Let's check if the name changed. This will be true for the first
// time the loop runs.
if($current_name != $row['FULLNAME'])
{
// If we are beginning, the if below will not run. But in subsequent
// records, it will add the acumulated array to the main result.
if($current_name != '') $result[] = $temp;
// The temp array will be populated with all data that DOES NOT change
// for the current name.
$temp = array('name' => $row['FULLNAME'],
'invoice' => $row['CUST_InvoiceNumber_020911544'],
'do_all_for' => $row['CUST_DoAllFor_021206685'],
'memo' => $row['CUST_Memo_021614200'],
'paymenttype' => $row['CUST_PAYMENTTYPE_123838203'],
'deposit' => $row['CUST_DEPOSIT_124139703'],
'datepaid' => date_normalizer($row['CUST_DATEPAID_124941578']),
);
// Update the current name.
$current_name = $row['FULLNAME'];
}
// The part that runs only on name changes has finished. From now on, we
// will take care of data which will be accumulated
// in a sub-array (until name changes and the block above resets it).
$temp['sub-array'][] =
array('service' => $row['CUST_Service_052400634'],
'date' => date_normalizer($row['CUST_ServiceDate_064616924']),
'service_amount' => $row['CUST_ServiceAmount_054855553']);
}
// After the loop, the last temp array needs to be added too.
$result[] = $temp;
This is the general concept: you will create a temporary array to hold the current name, inside which you will acummulate other data. Once the name changes, the acummulated data will be dumped to the main result, the temp array is reset, and a new acummulation begins.
I can't test the code right now, so it probably needs some fixes, but this approach works really well, and my point here is to show you the concept, so you can adapt it to your specific needs.
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 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)
);
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" } } }