Erlang / Erlmongo : Mongo arrays - mongodb

I can't find how to format my datas in Erlang and choose the encoding style provided by Erlmongo (default or mochijson) to be able to save datas as an Array in a Mongo document.
Example of a Mongo Document containing an array :
{
"_id" : MyId,
datas : [0,1,2,3]
}
This type of document is correct in Mongo format.
But how to create such a document with the erlmongo driver ?

I believe that you want to build an erlang property list. Your example JSON document in mongo might look like this as an erlang proplist:
[
{id, MyId},
{datas, [0, 1, 2, 3]}
]
Try this out and let me know if it works. There are a couple different mongo drivers for erlang, and I think I've only used emongo.
Also, some advice that you didn't ask for: "data" is already the plural form of "datum". You don't add an "s".
Hope this helps.
-tjw

Related

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

How to find by partial binary over BinData field in mongodb?

I have a BinData field in my mongo and I need to make a find over it with partial information.
Let's say that the bindata that I have looks like this:
{ "_id" : ObjectId("5480356518e91efd34e9b5f9"), "test" : BinData(0,"dGVzdA==") }
If I do this query I get the result:
> db.test.find({"test" : BinData(0,"dGVzdA==")})
{ "_id" : ObjectId("5480356518e91efd34e9b5f9"), "test" : BinData(0,"dGVzdA==") }
However I would like to find it with only a part of the binary object.
Is it possible?
Thanks!
"partial" is a vague term - if you're searching for a contiguous block of binary data (needle) at any point in the haystack, you're going to need a very different solution I think, maybe something based on a suffix tree / suffix array for binary data.
If you want to find binary data that starts with specific bytes, you might want to consider storing the data as hex or base64 encoded strings and use a rooted regex for index use. But that is fraught with its own perils (padding, endianness, etc.) and incredibly ugly...
Isn't there a way to store the binary data in a way that MongoDB understands it? That might be easier...

Mongo find by regex: return only matching string

My application has the following stack:
Sinatra on Ruby -> MongoMapper -> MongoDB
The application puts several entries in the database. In order to crosslink to other pages, I've added some sort of syntax. e.g.:
Coffee is a black, caffeinated liquid made from beans. {Tea} is made from leaves. Both drinks are sometimes enjoyed with {milk}
In this example {Tea} will link to another DB entry about tea.
I'm trying to query my mongoDB about all 'linked terms'. Usually in ruby I would do something like this: /{([a-zA-Z0-9])+}/ where the () will return a matched string. In mongo however I get the whole record.
How can I get mongo to return me only the matched parts of the record I'm looking for. So for the example above it would return:
["Tea", "milk"]
I'm trying to avoid pulling the entire record into Ruby and processing them there
I don't know if I understand.
db.yourColl.aggregate([
{
$match:{"yourKey":{$regex:'[a-zA-Z0-9]', "$options" : "i"}}
},
{
$group:{
_id:null,
tot:{$push:"$yourKey"}
}
}])
If you don't want to have duplicate in totuse $addToSet
The way I solved this problem is using the string aggregation commands to extract the StartingIndexCP, ending indexCP and substrCP commands to extract the string I wanted. Since you could have multiple of these {} you need to have a projection to identify these CP indices in one shot and have another projection to extract the words you need. Hope this helps.

How do you load an array from a BSON file on Pig using mongo-hadoop?

I'm trying to load data from a MongoDB BSON file into Pig using com.mongodb.hadoop.pig.BSONLoader (https://github.com/mongodb/mongo-hadoop/blob/master/pig/README.md) but I'm getting stuck. The data on MongoDB includes variable size arrays and I'm not sure how to load that into pig (as a tuple?). Here's a sample record from MongoDB:
{"_id": {"$oid": "52fbbca6e4b029a79cd17ff7"},
"field": "value",
"variableSizeArray": [
"value1",
"value2",
"valueN"
]
}
I've tried the following options and none of them seems to work:
raw = LOAD 'file:///tmp/teststreams.bson' using com.mongodb.hadoop.pig.BSONLoader('','field:chararray,variableSizeArray:()');
raw = LOAD 'file:///tmp/teststreams.bson' using com.mongodb.hadoop.pig.BSONLoader('','field:chararray,variableSizeArray:{T:(h:chararray)}');
Thanks for any help on this.
Finally figured it out. The way to do this is by not trying to specify the data type. This works:
raw = LOAD 'file:///tmp/teststreams.bson' using com.mongodb.hadoop.pig.BSONLoader('','field,variableSizeArray');

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