Full text search MongoDB/Mongoengine - mongodb

The new version of MongoDB allows Full Text Search. That part is running fine for me:
db.collection.runCommand('text',{search:<keyword>})
However, I'm not sure that it is possible to run it through python's mongoengine. Does anyone know if there is a way to run "runCommand" with mongoengine or a workaround?
(I'm using mongoengine for my project, I'd hate to have to drop it for pymongo as it would probably mean recoding many things.)
Thanks!

You can use MongoEngine by using pymongo directly eg:
class MyDoc(Document):
pass
coll = MyDoc._get_collection()
coll.database.command(
"text",
coll.name,
search="alice",
project={"name": 1, "_id": 0},
limit=10)

the question is old but I bumped on the same ask.
MongoEngine now enables text search directly.
First, create a text index on the required fields:
class MyDoc(Document):
document_name = StringField()
document_content = StringField()
meta = {
'indexes': [
{
'fields': [
'$document_name',
'$document_content'
]
}
]
}
Then, documents are searchable using the search_text function:
document = News.objects.search_text('testing')
MongoEngine Text Search documentation for further details.

Pymongo uses keyord arguments for this. See documentation.
In you case db.command('text', 'colection_name', seach='keyword').

Related

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

How do you search an array of documents in mongo dart?

How do I search an array of documents in mongodb using the dart programming language?
Im using https://pub.dartlang.org/packages/mongo_dart for the driver
The example db collection looks like this
"name": [
{"full":"Tyler Thompson",
"first":"Tyler",
"last":"Thompson"
}
]
Using the following query will help achieve your goal for the one name
String search = "Tyler";
{"name.full": {'\$regex': '${search}'}
Hope that helps! Couldn't find an example, so I thought I'd give one!:)
You can try to look at https://github.com/vadimtsushko/mongo_dart/blob/master/example/queries.dart
It is plausible that for your sample something like this would work:
coll.find(where.eq('name.full','Tyler'))
or maybe
coll.find(where.match('name.full','^Tyl[eo]r'))

Is there a findById shortcut for the MongoDB shell?

The most common thing I do in the mongo DB shell is find objects by ID, eg:
db.collection.find({_id: ObjectId("55a3e051dc75954f0f37c2f2"})
I do this over and over and I find having to wrap the id with ObjectId over and over gets old. I wish I had a findById-like shorthand form like what mongoose provides. I feel like the shell ought to be smart enough to figure out what I mean here for example:
db.collection.find("55a3e051dc75954f0f37c2f2")
How might I do this? Or are there any alternative ways of querying by id in the mongo shell?
Fortunately, you can extend the shell quite easily, for example by adding the following method to the ~/.mongorc.js file which is executed when you start the mongo client:
DBCollection.prototype.findById = function(id) {
return db.getCollection(this._shortName).find( { "_id" : ObjectId(id) } );
}
Then you can execute something like db.collection.findById("55a3e051dc75954f0f37c2f2")
The shorthand for find({_id: ObjectId("...")}) is find(ObjectId("...")).

MongoDB update query is failing silently

I have an app using Mongoid on top of MongoDB and an update is failing silently.
The code looks like this:
# Are we getting a new attribute? Yes we are!
p "attr first name = #{#attributes['first_name']}"
if user.update_attributes!(#attributes)
u = User.find(params[:id]).to_json
end
No exception is thrown in this code. So I looked at my MongoDB log and constructed this query based on what mongo is trying to do:
db.users.update({ "_id": "4d5561276ce886c496000001" }, { $set: { "first_name": "Erinamodobo" } }, false);
Now this does not cause any exceptions but when I grab the record that was supposed to be updated with this query:
db.users.find({"email":"escharling#somecompany.com"})
I see that the "first_name" attribute has not been updated.
Any idea why this could be happening? Sounds like something stupid.
Thanks!
You need to find out what
user.update_attributes!(#attributes)
is actually doing. This could be an issue with Mongoid. When you configure Mongoid, you can set up a logger. There you should be able to see what the driver is writing to MongoDB, and that should help answer your question. The next best thing is to post your code to the Mongoid mailing list (http://groups.google.com/group/mongoid) where people who work with Mongoid all the time will probably know what's going on.
Updating Mongoid to the latest rc.7 fixed this issue
Enable "safe mode" on your operations.

Performing regex queries with PyMongo

I am trying to perform a regex query using PyMongo against a MongoDB server. The document structure is as follows
{
"files": [
"File 1",
"File 2",
"File 3",
"File 4"
],
"rootFolder": "/Location/Of/Files"
}
I want to get all the files that match the pattern *File. I tried doing this as such
db.collectionName.find({'files':'/^File/'})
Yet I get nothing back. Am I missing something, because according to the MongoDB docs this should be possible? If I perform the query in the Mongo console it works fine, does this mean the API doesn't support it or am I just using it incorrectly?
If you want to include regular expression options (such as ignore case), try this:
import re
regx = re.compile("^foo", re.IGNORECASE)
db.users.find_one({"files": regx})
Turns out regex searches are done a little differently in pymongo but is just as easy.
Regex is done as follows :
db.collectionname.find({'files':{'$regex':'^File'}})
This will match all documents that have a files property that has a item within that starts with File
To avoid the double compilation you can use the bson regex wrapper that comes with PyMongo:
>>> regx = bson.regex.Regex('^foo')
>>> db.users.find_one({"files": regx})
Regex just stores the string without trying to compile it, so find_one can then detect the argument as a 'Regex' type and form the appropriate Mongo query.
I feel this way is slightly more Pythonic than the other top answer, e.g.:
>>> db.collectionname.find({'files':{'$regex':'^File'}})
It's worth reading up on the bson Regex documentation if you plan to use regex queries because there are some caveats.
The solution of re doesn't use the index at all.
You should use commands like:
db.collectionname.find({'files':{'$regex':'^File'}})
( I cannot comment below their replies, so I reply here )