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

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'))

Related

jessenger mongodb case insensitive query search

I have 1 issue with mongodb query search with exact values. i want to get collections irrespective of case sensitive. for this i found some querys like below. its working fine.
db.applications.find({"blocks.HOSPITAL_INFO.data.name": new RegExp('^VIKRAM$', 'i')});
in laravel i am using jessengers. . above query i can write as raw query in laravel.
but my issue is when ever i am using $In:{'a','b'} like this how can i write regex for this. FYI 'a','b' are dynamic array values. so how can i write regex for these array values?
The query in MongoDB would be something like this:
db.applications.find({"blocks.HOSPITAL_INFO.data.name":
{$in:[new RegExp('^a$', 'i'),new RegExp('^b$', 'i')]}});
OR, alternatively:
db.applications.find({"blocks.HOSPITAL_INFO.data.name":
{$in:[/^a$/i,/^b$/i]}});
...where a & b are your dynamic variables.
I'm not very familiar with Laravel, but I'm guessing it would look something like this:
$applications = Application::whereIn('blocks.HOSPITAL_INFO.data.name',
[new MongoRegex('^a$/i'), new MongoRegex('^b$/i')])->get();

Find All Objects Created Before Specified Date

Mongo has a nice feature that tells you when a document was created.
ObjectId("53027f0adb97425bbd0cce39").getTimestamp() = ISODate("2014-02-17T21:28:42Z")
How would I get about finding all documents that were created before lets say February 10th 2014? Searched around but doesn't seem like this question comes up. Any help is appreciated! Thanks!
You mean something like this?
db.YOUR_COLLECTION.find({YOUR_DATE_FIELD: { "$lt": ISODate("2014-02-10") }})
Guess that you have to make the same as JoJo recommended:
Convert a date to an ObjectId
Filter ID using $lt and returned ObjectId
Using pymongo you can do something like this:
gen_time = datetime.datetime(2014, 2, 10)
dummy_id = ObjectId.from_datetime(gen_time)
result = collection.find({"_id": {"$lt": dummy_id}})
Reference: http://api.mongodb.org/python/1.7/api/pymongo/objectid.html

Reverse search at mongodb

I have tried to search around for clues but didn't find any.
Please let me ask a question about reverse search for mongodb.
I have a collection that contain a phone prefix, let say bellow.
{"prefix":"1234"}
and i am trying to make it hit with value like 12341111 or 12342222 ...
in MySQL i can do it with below SQL.
SELECT * from phoneprefix where ‘12341111’ like concat(prefix,’%’)
Is there anyway to make it possible at mongodb ?
Thanks for reading my post and please have a nice day !
The only way I can see to do this is to use the performance-challenged $where operator:
db.phoneprefix.find({$where: function() {
return (new RegExp('^' + this.prefix)).test('12341111');
}});
You could use $regex in MongoDB.

Full text search MongoDB/Mongoengine

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').

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.