Dynamic query in couchbase? - nosql

I started using couchbase,
i like it a lot but one thing i cant find,
making a dynamic query,
{
"sender_name": "roman",
"sender_id": 123,
"content": "Hello World"
}
Now i want to query for document where "sender_id" = ?.
It can be any number,
Regular view with doc and meta cant help me because i dont know the value,
I should expect any sender_id.
Hope you can help me, thanks alot.

Ok , with Couchbase you can write Map&Reduce functions, and create views. The map functions accept parameters. I am not quite familiar with writing a Map function, but from couchbase.com I think this map function will do your job.
function(doc, meta)
{
emit(doc.sender_id, [doc.content]);
}
And your query would be ?key=["123"]
Go through these links
http://hardlifeofapo.com/creating-an-e-commerce-platform-using-couchbase-2/
http://hardlifeofapo.com/basic-couchbase-querying-for-sql-people/
http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writing-sql-where.html

Related

How do I perform aggregate queries using SumoLogic APIs

I am trying to perform aggregate queries using SumoLogic APIs as mentioned here.
Something like:
_view = <some_view> | where sourceCategory matches \"something\" | sum(field) by sourceCategory
This works just fine in the Sumo GUI. I get a field in result called "_sum" which gives me the desired result.
However the same doesn't work when I do it using the SUMO APIs. If I create a job with this body:
{
"query": "_view = <some_view> | where sourceCategory matches "something" | sum(field) by sourceCategory",
"from": "start_timestamp",
"to": "end_timestamp",
"timeZone": "some_timezone"
}
I call the "v1/search/jobs" POST method with the above body and I do GET "v1/search/jobs/{job_id}" till the state is "DONE GATHERING RESULTS". Then I do "v1/search/jobs/{job_id}/messages". I was expecting to see aggregated values in the result, but instead I see something similar to:
{
"fields":[
{
"name":"_messageid",
"fieldType":"long",
"keyField":false
}, ...
],
"messages":[
{
"map":{
"_receipttime":"1359407350899",
"_size":"549",
"_sourcecategory":"service",
"_sourceid":"1640",
"the_field_i_mentioned":"not-aggregated-value"
"_messagecount":"2044"
}
}, ...
]
]
Thanks for going through my question. Any advices / work-arounds are appreciated. I don't really want to iterate manually through all items and calculate the sum. I'd prefer to do it on SumoLogic side itself. Thanks Again!
Explanation
Similar as in the User Interface, in the API for log searches you get both raw results (also referred to as messages) and the aggregate results (also referred to as records).
(Obviously, the latter are only returned if there's any aggregation in the query. In your case there is.)
Actual suggestion
Then I do "v1/search/jobs/{job_id}/messages"
Try /records instead.
See the docs for "Paging through the records found by a Search Job"
Disclaimer: I am currently employed by Sumo Logic.

Dart/Flutter Typeahead for JSON API with indexed list

Can someone give me an example of how to use the typeahead to autofill a search field
and put it in the URL /$typedata/ where it calls an API and returns a JSON file that looks like the below? All of the examples I see are nice and neat with the booktitle: "Leaving";
author: "whoever";
publisher: "Phantom";
or there is a list with the variables already identified ["Leaving", "whoever", "Phantom"]
I can't figure out how to put all of these elements together. I need to extract the "compound: []
up to six variables.
It's several different pieces and I'm new. I can't seem to get past the compound with mutliple variables. I know I need an index and to call the index by number but I can't seem to get it all together.
Any help is appreciated.
{
"status": {
"code": 0
},
"total": 6,
"dictionary_terms": {
"compound": [
"aspirin",
"Aspirina",
"AspirinTest2",
"ASPIRIN (MART.)",
"ASPIRIN COMPONENT OF AXOTAL",
"ASPIRIN COMPONENT OF AZDONE"
]
}
}
btw: I'm new to this and I'm sure I'm missing something important. Thanks for any help!!!

Creating a filter in mongo-go-driver for .FindOne

I'm trying to check a collection to see if there is at least one documents that match a specific set of values.
I've tried reading the documentation at https://github.com/mongodb/mongo-go-driver#usage, but I can't seem to find much help there. I'm pretty new to MongoDB & Go, I believe that this is more a problem of my lack of experience.
Here is a sample query from Studio 3T that I'm trying to run with mongo-go-driver:
db.getCollection("events").find(
{
"event.eventType" : "OSR",
"context.vehicleId" : NumberInt(919514),
"ts" : {
"$gte" : ISODate("2019-06-21T21:38:43.022+0000")
}
}
).limit(1);
It seems that the context.FindOne method will do what I want (and eliminating the need for the .limit(1)). I thought that it would be straight forward to "port" this to Go and the mongo-go-driver.
I can sort of make this work, for example I have the following which will find me all the OSR:
var query = &bson.D{
{"event.eventType", "OSR"},
}
result := bson.D{}
e := collection.FindOne(context.TODO(), query).Decode(&result)
This will return me one document. Now, if I want to include the vehicleId value, and I update the query to:
var query = &bson.D{
{"event.eventType", "OSR"},
{"context.vehicleId", 919514},
}
No documents are returned. I haven't bother to expand query to include the ts field yet.
I would expect at to still have at least one document returned, but nothing is showing up. Does anybody have some tips, suggestions or guidance on what I'm doing wrong (or perhaps how I can do this better)?
Not quite sure, but have you tried with bson.M instead of bson.D?
It seems like it's working for me at least.
query := &bson.M{
"event.eventType": "OSR",
"context.vehicleId": 919514,
}
Please refer to the docs for more information.
Also, like #owlwalks said, are you sure, you're in the right collection?

Mongo query with regex fails when backslash\newline is there in a field

Hi I have a field in a user collection called "Address".User saving their address from a textarea in my application. mongodb convert it to new line like following.
{
"_id": ObjectId("56a9ba0ffbe0856d4f8b456d"),
"address": "HOUSE NO. 3157,\r\nSECTOR 50-D",
"pincode": "",
},
{
"_id": ObjectId("56a9ba0ffbe0856d4f8b456d"),
"address": "HOUSE NO. 3257,\r\nSECTOR 50-C",
"pincode": "",
}
So now When I am running a search query on the basis of "address".Like following:
guardianAdd = $dm->getRepository('EduStudentBundle:GuardianAddress')->findBy(array(
'address' => new \MongoRegex('/.*' .$data['address'] . '.*/i'),
'isDelete' => false
));
echo count($guardianAdd);die;
it does not give any result. My Searchi key word is : "HOUSE NO.3157 SECTOR 50-D".
However if I am searching using like: HOUSE NO. 3157 its giving correct result.
Please advice how to fix this.Thanks in advance
First of all, trailing .* are redundant. regexps /.*aaa.*/ and /aaa/ are identical and match the same pattern.
Second, you probably need to use multiline modifier /pattern/im
Finally, it is not quite clear what you want to fix. The best think you can do is to provide some basic explanation of regex syntax in the search form, so users can search properly, e.g. HOUSE NO.*3157.*SECTOR 50-D to get best results.
You can make some bold assumptions and build the pattern with something like
$pattern = implode('\W+',preg_split('/\W+/', $data['address']))
which will give you a regexp HOUSE\W+NO\W+3157\W+SECTOR\W+50\W+D for different kind of HOUSE NO.3157 SECTOR 50-D requests, but it will cut all the regex flexibility available with bare input, and eventually will result with unexpected response anyway. You can follow this slippery slope and end up with your own query DSL to compile to regex, but I doubt it can be any better or more convenient than pure regex. It will be more error prone for sure.
Asking right question to get right answers is true not only on SO, but also in your application. Unfortunately there is no general solution to search for something that people have in mind, but fail to ask. I believe that in your particular case best code is no code.

Querying an embedded document with javascript

I have documents in mongodb that i'm accessing with mongoid that looks like this:
{
"first_name": "Clarke",
"last_name": "Kent",
"vault_info": {
"container": "names",
"created_at": "2013-12-09T23:18:07.963Z",
"updated_at": "2013-12-09T23:18:07.963Z",
"vault_id": "4dc08baa97"
}
}
I want to be able to query for it using the for_js method like this:
Model.for_js('this.vault_info.vault_id=="5088de6f12"')
If there is a single document in the database this works. If there is more than one it gives this error:
"TypeError: Cannot read property 'vault_id' of undefined near '==\"5088de6f12\"' "
Any help would be appreciated.
Robert
It looks like the new documents you're adding don't have a vault_info property on them. That means that this.vault_info evaluates to undefined, and you can't access any property (including vault_id) of undefined.
If you don't have to use the for_js method, just use the where method like this:
Model.where('vault_info.vault_id' => '5088de6f12')
Mongo intelligently filters out all documents that lack the vault_info property, so you don't get any errors about accessing properties of undefined. JavaScript doesn't help you out like that, so if you do absolutely have to use the for_js method, you'll have to check each step of the way. That'd look something like:
Model.for_js('this.vault_info && this.vault_info.vault_id=="5088de6f12"')
I'd encourage you to use the where method, though. The less JavaScript you send to MongoDB the better.