Intersystems cache db - specific ROWSPEC dynamically - intersystems-cache

I can do this:
Query All() As %Query(CONTAINID = 1, ROWSPEC = "Title:%String,Author:%String")
{
}
But I need to specify ROWSPEC dynamically. I have globals like this:
^glob("title1","author1","xxKZ1") = "val1"
^glob("title1","author1","ssn","xyPO2") = "val2"
^glob("title2","author2","xxII8") = "val3"
^globNext("key1") = "val1"
^globNext("key1","key2") = "val2"
So I need to dynamically create structure of query row. For ^glob I need have something like this:
Query All() As %Query(CONTAINID = 1, ROWSPEC = "Prop1:%String, Prop2:%String, Prop3:%String, Prop4:%String, Val:%String")
{
}
For ^globNext I need something like:
Query All() As %Query(CONTAINID = 1, ROWSPEC = "Prop1:%String, Prop2:%String)
{
}
Is it possible to reach it?

No, it is not possible, because number of columns have to be fixed. But as your code generate result, you can define some columns like Prop1, Prop2...PropN, and in result return as many columns as you need, and any last columns just well be null. And after that in your client-side code you cant get access by its like Value.

Related

Strict parsing into POJOs with KMongo

When I find documents in my collections and parse them into POJOs, I would like to see exceptions, if additional keys are available in the MongoDB, that do not correspondent to my POJO.
Can't find a way to configure that.
What I do
data class MyPojo(var a: Int)
val mongoClient = KMongo.createClient(...)
val collection = mongoClient...
val results = collection.aggregate<MyPojo>(...)
and if a result document is
{ "a": 1, "b": 2 }
What I get:
MyPojo(a=1)
I would like to see an exception of sort
kotlinx.serialization.json.JsonDecodingException: Invalid JSON...: Encountered an unknown key b
Does anyone know how to do that?
You have to specify strictMode = true in your JsonConfiguration for example:
install(ContentNegotiation) {
serialization(
contentType = ContentType.Application.Json,
json = Json(
JsonConfiguration(
strictMode = true,
prettyPrint = true
)
)
)
}

mongoengine filter on DictField's dynamic keys

class UserThings(DynamicDocument):
username = StringField()
things = DictField()
dcrosta_things = UserThings(username='dcrosta')
dcrosta_things.things['foo'] = 'bar'
dcrosta_things.things['bad'] = 'quack'
dcrosta_things.save()
Results in a MongoDB document like:
{ _id: ObjectId(...),
_types: ["UserThings"],
_cls: "UserThings",
username: "dcrosta",
things: {
foo: "bar",
baz: "quack"
}
}
Im using mongoengine and I'm not able to query on dict field's keys.
for e.g I have a list of things thing_list = ['foo', 'faa', 'baz', 'xyz']
and I want to filter all UserThings that contains anyof these things...
something like .. UserThings.objeect.filter(things__in=thing_list)
definitely this wont work. IS there any way to perform filtering on variable/dynamic keys on dictfield. if not, can we do it using pymongo/raw query?

How to update nested object (i.e., doc field having object type) in single document in mongodb

I have doc collection which have object type field named price (i.e., see below), I just want to update/insert that field by adding new key value pairs to it.
suppose i have this as collection (in db):
[
{
_id: 1,
price: {
amazon: 102.1,
apple: 500
}
},
....
....
];
Now I want to write an query which either update price's or inserts if not exist in price.
let's suppose these as input data to update/insert with:
var key1 = 'ebay', value1 = 300; // will insert
var key2 = 'amazon', value2 = 100; // will update
assume doc having _id: 1 for now.
Something like $addToSet operator?, Though $addToSet only works for array & i want to work within object).
expected output:
[
{
_id: 1,
price: {
amazon: 100, // updated
apple: 500,
ebay: 300 // inserted
}
},
....
....
];
How can i do/achieve this?
Thanks.
You could construct the update document dynamically to use the dot notation and the $set operator to do the update correctly. Using your example above, you'd want to run the following update operation:
db.collection.update(
{ "_id": 1 },
{
"$set": { "price.ebay": 300, "price.amazon": 100 }
}
)
So, given the data input, you would want to construct an update document like { "price.ebay": 300, "price.amazon": 100 }
With the inputs as you have described
var key1 = 'ebay', value1 = 300; // will insert
var key2 = 'amazon', value2 = 100; // will update
Construct the update object:
var query = { "_id": 1 },
update = {};
update["price."+key1] = value1;
update["price."+key2] = value2;
db.collection.update(query, {"$set": update});

How to append a key:value to a MongoDB cursor?

Is it possible to append a key:value to a MongoDB cursor?
I tried this:
cursor = collection.find(query,projection)
cursor['my_message'] = "value here" # trying to add key:value here
But it doesn't seem to work (500).
In more context, this works:
dbname = 'my_db'
db = connection[dbname]
collection = db.my_collection
query = {'key_1': my_var}
projection = {'key_2':1}
cursor = collection.find(query,projection)
response.content_type = 'application/json'
return dumps(cursor)
This doesn't:
dbname = 'my_db'
db = connection[dbname]
collection = db.my_collection
query = {'key_1': my_var}
projection = {'key_2':1}
cursor = collection.find(query,projection)
cursor['my_message'] = "value here" # trying to add key:value here
response.content_type = 'application/json'
return dumps(cursor)
Edit: And just to visualise what is being returned successfully (without the appended value), it is something like:
[{ document_1 },{ document_2 },{ document_3 }]
And I expect it to look something like:
["my_message":"value here",{ document_1 },{ document_2 },{ document_3 }]
Edit: I tried the following as an alternative and also got a 500.
entries = []
cursor = collection.find(query,projection)
for entry in cursor:
entries.append(entry)
entries['my_message'] = "value here"
response.content_type = 'application/json'
return dumps(entries)
Really, WiredPrarie answered this for you right at the beginning, and everyone is saying the same thing.
We know what you want to do. You want your serialized response to be sent back with some information you want to put in and then the resultset. I also presume that you want to use these results and your other data, likely loaded into some JavaScript processing store.
I have never seen anything that didn't expect some sort of structure like:
{
"result": "ok",
"meta": [{ akey: "avalue"}, {bkey: "bvalue"}],
"results:[ // this is your 'entries' value here
{ document_1 },
{ document_2 },
{ document_3 },
....
So what everyone is saying is embed your entries into another structure that you are going to serialize and return. By trying to push your other keys into the entries list you are doing it the wrong way around.

How to access this._id in map function in MongoDB MapReduce?

I'm doing a MapReduce in Mongo to generate a reverse index of tokens for some documents. I am having trouble accessing document's _id in the map function.
Example document:
{
"_id" : ObjectId("4ea42a2c6fe22bf01f000d2d"),
"attributes" : {
"name" : "JCDR 50W38C",
"upi-tokens" : [
"50w38c",
"jcdr"
]
},
"sku" : "143669259486830515"
}
(The field ttributes['upi-tokens'] is a list of text tokens I want to create reverse index for.)
Map function (source of the problem):
m = function () {
this.attributes['upi-tokens'].forEach(
function (token) { emit(token, {ids: [ this._id ]} ); }
); }
Reduce function:
r = function (key, values) {
var results = new Array;
for (v in values) {
results = results.concat(v.ids);
}
return {ids:results};
}
MapReduce call:
db.offers.mapReduce(m, r, { out: "outcollection" } )
PROBLEM Resulting collection has null values everywhere where I'd expect an id instead of actual ObjectID strings.
Possible reason:
I was expecting the following 2 functions to be equivalent, but they aren't.
m1 = function (d) { print(d['_id']); }
m2 = function () { print(this['_id']); }
Now I run:
db.offers.find().forEach(m1)
db.offers.find().forEach(m2)
The difference is that m2 prints undefined for each document while m1 prints the ids as desired. I have no clue why.
Questions:
How do I get the _id of the current object in the map function for use in MapReduce? this._id or this['_id'] doesn't work.
Why exactly aren't m1 and m2 equivalent?
Got it to work... I made quite simple JS mistakes:
inner forEach() in the map function seems to overwrite 'this' object; this is no longer the main document (which has an _id) but the iterated object inside the loop)...
...or it was simply because in JS the for..in loop only returns the keys, not values, i.e.
for (v in values) {
now requires
values[v]
to access the actual array value. Duh...
The way I circumvented mistake #1 is by using for..in loop instead of ...forEach() loop in the map function:
m = function () {
for (t in this.attributes['upi-tokens']) {
var token = this.attributes['upi-tokens'][t];
emit (token, { ids: [ this._id ] });
}
}
That way "this" refers to what it needs to.
Could also do:
that = this;
this.attributes['upi-tokens'].forEach( function (d) {
...
that._id...
...
}
probably would work just fine.
Hope this helps someone.