Is it possible to iterate a mongo cursor twice? - mongodb

In other words, is there a way to rewind it to the beginning?
EDIT
I am using mongo shell and pymongo.

The Ruby API has the rewind! method that does exactly what you want.
The Python API also has the cursor.rewind() method.
The PHP API also has the cursor.rewind() method.
However, neither the Java or C++ APIs have the rewind method. All can be found from the official API page.

Cursor in pymongo has .rewind() method, you can refer to sample code from previous question with answer that apply.
Native mongo shell api, however, doesn't provide such method, see method help() on DBQuery object prototype.:
> db.collection.find().help()
find() modifiers
.sort( {...} )
.limit( n )
.skip( n )
.count() - total # of objects matching query, ignores skip,limit
.size() - total # of objects cursor would return, honors skip,limit
.explain([verbose])
.hint(...)
.showDiskLoc() - adds a $diskLoc field to each returned object
Cursor methods
.forEach( func )
.map( func )
.hasNext()
.next()

You can use cursor.reset();
For PHP : $cursor->reset();
then run your foreach($cursorData as $data) any time after resetting.

Related

How do I create an array for an updateOne() call in mongoc (C libarary for Mongodb)?

I am completely mystified (and supremely frustrated). How do I create this call using the mongoc library?
I have the following doc structure in the collection
{_id: myOID,
subsriptions: {
newProducts: true,
newBlogPosts: true,
pressReleases: true,
}
}
I want to remove one of the subscriptions, for example, the user no longer wants to receive press releases from me.
This works in the mongo shell. Now I need to do it in C code
updateOne({_id: myOID}, [{'$unset': 'subscriptions.pressReleases'}], {})
Note how the update parameter in the Mongo shell is an anonymous array. I need to do that for the bson passed in as the update parameter in the mongoc_collection_update_one() API call.
The C code for updateOne is
mongo_status = mongoc_collection_update_one (mongo_collection,
mongo_query,
mongo_update,
NULL, /* No Opts to pass in */
NULL, /* no reply wanted */
&mongo_error);
Also note that in the aggregate() API, this is done with
{"pipeline" : [{'$unset': 'elists.lunch' }] }
Neither the updateOne() shell function nor the mongoc_collection_update_one() API call accept that, they want just the array.
How do I create the bson to use as the second parameter for mongoc_collection_update_one() API call?
Joe's answer works and I am able to accomplish what I need to do.
The $unset update operator takes an object, just like $set.
updateOne({_id: myOID},{'$unset':{'subscriptions.pressReleases': true}})
OR perhaps even better
updateOne({_id: myOID},{'$unset':{'subscriptions.pressReleases': {'$exists': true}}})
which will remove the subscription flag no matter what the value is for that field.
Doing it this way does not require an anonymous array (which I still don't know how to create).

MongoDB findOneAndReplace log if added as new document or replaced

I'm using mongo's findOneAndReplace() with upsert = true and returnNewDocument = true
as basically a way to not insert duplicate. But I want to get the _id of the new inserted document (or the old existing document) to be passed to a background processing task.
BUT I also want to log if the document was Added-As-New or if a Replacement took place.
I can't see any way to use findOneAndReplace() with these parameters and answer that question.
The only think I can think of is to find, and insert in two different requests which seems a bit counter-productive.
ps. I'm actually using pymongo's find_one_and_replace() but it seems identical to the JS mongo function.
EDIT: edited for clarification.
Is it not possible to use replace_one function ? In java I am able to use repalceOne which returns UpdateResult. That has method for finding if documented updated or not. I see repalce_one in pymongo and it should behave same. Here is doc PyMongo Doc Look for replace_one
The way I'm going to implement it for now (in python):
import pymongo
def find_one_and_replace_log(collection, find_query,
document_data,
log={}):
''' behaves like find_one_or_replace(upsert=True,
return_document=pymongo.ReturnDocument.AFTER)
'''
is_new = False
document = collection.find_one(find_query)
if not document:
# document didn't exist
# log as NEW
is_new = True
new_or_replaced_document = collection.find_one_and_replace(
find_query,
document_data,
upsert=True,
return_document=pymongo.ReturnDocument.AFTER
)
log['new_document'] = is_new
return new_or_replaced_document

call custom python function on every document in a collection Mongo DB

I want to call a custom python function on some existing attribute of every document in the entire collection and store the result as a new key-value pair in that (same) document. May I know if there's any way to do that (since each call is independent of others) ?
I noticed cursor.forEach but can't it be done just using python efficiently ?
A simple example would be to split the string in text and store the no. of words as a new attribute.
def split_count(text):
# some complex preprocessing...
return len(text.split())
# Need something like this...
db.collection.update_many({}, {'$set': {"split": split_count('$text') }}, upsert=True)
But it seems like setting a new attribute in a document based on the value of another attribute in the same document is not possible this way yet. This post is old but the issues seem to be still open.
I found a way to call any custom python function on a collection using parallel_scan in PyMongo.
def process_text(cursor):
for row in cursor.batch_size(200):
# Any complex preprocessing here...
split_text = row['text'].split()
db.collection.update_one({'_id': row['_id']},
{'$set': {'split_text': split_text,
'num_words': len(split_text) }},
upsert=True)
def preprocess(num_threads=4):
# Get up to max 'num_threads' cursors.
cursors = db.collection.parallel_scan(num_threads)
threads = [threading.Thread(target=process_text, args=(cursor,)) for cursor in cursors]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
This is not really faster than cursor.forEach (but not that slow either), but it helps me execute any arbitrarily complex python code and save the results from within Python itself.
Also if I have an array of ints in one of the attributes, doing cursor.forEach converts them to floats which I don't want. So I preferred this way.
But I would be glad to know if there're any better ways than this :)
It is quite unlikely that it will ever be efficient to do this kind of thing in python. This is because the document would have to make a round trip and go through the python function on the client machine.
In your example code, you are passing the result of a function to a mongodb update query, which won't work. You can't run any python code inside mongodb queries on the db server.
As the answer to you linked question suggests, this type of action has to be performed in the mongo shell. e.g:
db.collection.find().snapshot().forEach(
function (elem) {
splitLength = elem.text.split(" ").length
db.collection.update(
{
_id: elem._id
},
{
$set: {
split: splitLength
}
}
);
}
);

Wrong distance in geonear method with Doctrine MongoDB ODM

I'm using DoctrineMongoDBBundle with Symfony2 and I've a problem with geocoordinates. This works fine but when the longitude is for example like that: 0.635467 the code doesn't work. I have more geocoordinates and only fails when it begins with 0. and the distance field is NULL.
This is my code:
$locations = $dm->createQueryBuilder('MyBundle:Location')
->field('id')->in($arrayIds)
->field('geocoordinates')
->geoNear($geocodes['lat'],$geocodes['lon'])
->getQuery()->execute()->toArray();
I'm following this link: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/geospatial-queries.html but with the geonear method.
The geoNear() query builder method is not intended to be used on a field. near() is the builder method that would follow a field() focus. You can see what both of these builder methods do in Builder.php within the doctrine/mongodb project. Note that geoNear() changes the query type (similar to what update() does). The query type is then checked in Query.php (follow the switch statement) and determines how we issue the query on the collection. Some are actual query operations, but things like map/reduce and geoNear are commands.
See if the following code works:
$dm->createQueryBuilder('MyBundle:Location')
->geoNear($geocodes['lat'],$geocodes['lon'])
->field('id')->in($arrayIds)
->getQuery()->execute()->toArray();
If not, please debug the values that Query.php passes to the Collection::near() method. Alternatively, you can debug the entire query array generated by the builder by using the Query::getQuery() method.

MongoDB map/reduce "NoMethodError: undefined method `map_reduce' for #<Moped::Collection"

I am trying to use map_reduce on a collection, via the ruby console , but I am getting "NoMethodError: undefined method `map_reduce' for #
results = Thing.collection.map_reduce(map, reduce, out: "vr")
Map Reduce in Mongoid 3 works slightly differently. The syntax you have would work for the mongo ruby driver. In Mongoid 3, you call this off the class or criteria, like the following:
From a criteria:
Model.where(field: value).map_reduce(map, reduce).out(inline: true)
From a class:
SomeClass.map_reduce(map, reduce).out(replace: "mr-results").each do |document|
#do something
end
You can find more information on this in the Mongoid docs