Why GitHub?
Business
Explore
Marketplace
Pricing
Sign inSign up
Watch 81
Star 566
Fork 147
mongodb/mongo-php-driver
Dismiss
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage
projects, and build software together.
Sign up
New issue
Comments
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
2 participants
Description
How to get a random document from a very big collection (10000000 documents) using the
new mongodb-php library.
Till now i have tried :
$limit = 1;
$skip = mt_rand(0, $collection->count());
$skip = $skip < 0 ? 0 : $skip;
$options = array(
"limit" => $limit,
"skip" => $skip
);
$collection->find( $conditions, $options );
But it doesn't give me any record at all.
Environment
OS : centos 7
Mongo version : 3.6
mongo php drivers : 1.4
Member
jmikola commented on Feb 28
There is also an old cookbook article on the subject, although I'm not sure if it's published
anywhere (that links to the RST source).
Note that using skip with a large offset is very inefficient, as it forces the server to scan through
all skipped documents. This applies to your example as well as using limit and skip to
implement crude pagination.
@jmikola thanks mate.I appreciate for your time that you took out to see my issue.
I did resolve it via $sample aggregation.
Though i am wondering now :
$this->collection->update(
array( 'field1' => $field1_value ),
array(
array('some_field' => $some_field_value),
array('field_usage_timestamp' => time()),
array('$inc' => array('field_usage' => 1))
)
);
Basically i want to find a document using some_field and then try to increment field_usage if
document has this field and if not insert this field field_usage with a value of 1.
Any ideas would be highly appreciated. :)
Member
jmikola commented on Mar 1 •
edited
MongoDB\Collection does not have an update() method, so please make sure you're up to date
on the library documentation. Differences from the legacy mongo extension's API are discussed in
the library's Upgrade Guide. While the MongoDB shell does have a multi-
purpose db.collection.update() method to allow for updateOne, updateMany, and replaceOne
operations, the drivers currently provide explicit methods for those three variations of
the update command.
Furthermore, the second argument to update() in your example would not be a valid argument
for either an atomic update or full-document replacement. I would suggest you read through
the Update Documentstutorial in the MongoDB manual. You can select "PHP" at the top of the
document to display syntax examples for the PHP library.
I find a document using some_field
In that case, some_field should be used the first argument, rather than the second. You're
currently matching a document only by the value of field1.
To explain why the second argument is invalid, I'll expand it to how it would serialize in BSON:
[
0 => [ 'some_field' => $some_field_value ],
1 => [ 'field_usage_timestamp' => time() ],
2 => [ '$inc' => [ 'field_usage' => 1 ] ],
]
This would not be a valid replacement document because $inc appears as a field key (document
fields cannot start with "$"). If $inc did not appear, the replacement document would be valid,
but you'd be overwriting the original document so that it only kept is _id field followed by the
three numeric fields (the integers would end up as strings in BSON when added to the root
document).
That said, if you were intending to
instead set some_field and field_usage_timestamp and incrementfield_usage, the following
update document could be used:
[
'$set' [
'some_field' => $some_field_value,
'field_usage_timestamp' => time(),
],
'$inc' => [ 'field_usage' => 1 ],
]
try to increment field_usage if document has this field and if not insert this field field_usage with
a value of 1.
Per $inc: Behavior in the MongoDB manual, that is exactly what already happens.
@jmikola thanks mate...that was a typo in question for finding a document using some_field.
I got it working this way, but i think i should have asked you the main issue in first place can you
kindly give it a look.
https://stackoverflow.com/questions/49053539/how-to-group-mongodb-documents-in-php
Member
jmikola commented on Mar 7 •
edited
I replied in the Stack Overflow thread. Closing this out as I believe the question here has been
addressed.
Contact GitHub
Pricing
API
Training
Blog
About