Issue to fetch data by condition - ydn-db

again I need help in ydn db. I have an object store where objects has these attributes:
id, published, tempId, sonyId
I set published, tempId and sonyId to the indexes :
keyPath : 'id',
name : 'XYStore'
indexes : [{ keyPath : ['published', 'sonyId', 'tempId'}]
Now my query to filter by published and sonyId does not work:
db
.from('XYStore')
.where('sonyId, published', '=', ['15', '1'])
.list()
.done(function(records) {
console.log(records);
});
What did I made wrong? If I remove 'tempId' from indexes than everything works fine.
I add tempId to the indexes because in another fetch method I call all entries by tempId:
db
.from('XYStore')
.where('tempId', '=', '8')
.list()
.done(function(records) {
});
I am not really sure. What did I made wrong?
Thanks in advcance.

You have to index each query as follow:
indexes : [{
keyPath : ['published', 'sonyId']
}, {
keyPath : 'tempId'
}],

Related

Cosmos SQL API: filter based an array entry

Given the json
{
"fileIds": [
"171824c7-1485-4b35-9418-2b40aea8fa48",
"b158e946-621f-431e-926c-57577e3e0b6b"
],
"eventType": 8,
"description": "File deleted from original storage account",
"registerTimeUtc": "0001-01-01T00:00:00",
"customerId": "c7b00078-8fd7-49ad-989a-2fabcb767f6e",
"applicationId": "266c6ca3-e77c-4233-b780-b8dd69aa7349"
...
}
I need to check if an entry exists based on a given applicationId, eventType and fileId and I do not know how to write the query based on fileId to check its existence in the array list.
Please try the following query:
SELECT * FROM Root r where
r.applicationId = '266c6ca3-e77c-4233-b780-b8dd69aa7349' and
r.eventType = 8 and
ARRAY_CONTAINS(r.fileIds, '171824c7-1485-4b35-9418-2b40aea8fa48')
To search for an item in an array, you will need to use ARRAY_CONTAINS function.

How to update a nested object inside an array in DynamoDB

Consider the following document item / syntax in a DynamoDB table:
{
"id": "0f00b15e-83ee-4340-99ea-6cb890830d96",
"name": "region-1",
"controllers": [
{
"id": "93014cf0-bb05-4fbb-9466-d56ff51b1d22",
"routes": [
{
"direction": "N",
"cars": 0,
"sensors": [
{
"id": "e82c45a3-d356-41e4-977e-f7ec947aad46",
"light": true,
},
{
"id": "78a6883e-1ced-4727-9c94-2154e0eb6139",
}
]
}
]
}
]
}
My goal is to update a single attribute in this JSON representation, in this case cars.
My approach
I know all the sensors IDs. So, the easiest way to reach that attribute is to find, in the array, the route which has a sensor with any of the ids. Having found that sensor, Dynamo should know which object in the routes array he has to update. However, I cannot run this code without my condition being rejected.
In this case, update attribute cars, where the route has a sensor with id e82c45a3-d356-41e4-977e-f7ec947aad46 or 78a6883e-1ced-4727-9c94-2154e0eb6139.
var params = {
TableName: table,
Key:{
"id": "0f00b15e-83ee-4340-99ea-6cb890830d96",
"name": "region-1"
},
UpdateExpression: "set controllers.intersections.routes.cars = :c",
ConditionExpression: ""controllers.intersections.routes.sensors.id = :s",
ExpressionAttributeValues:{
":c": 1,
":s": "e82c45a3-d356-41e4-977e-f7ec947aad46"
},
ReturnValues:"UPDATED_NEW"
};
docClient.update(params, ...);
How can I achieve this?
Unfortunately, you can't achieve this in DynamoDB without knowing the array index. You have very complex nested structure. The DynamoDB API doesn't have a feature to handle this scenario.
I think you need the array index for controllers, routes and sensors to get the update to work.
Your approach may work in other databases like MongoDB. However, it wouldn't work on DynamoDB. Generally, it is not recommended to have this complex structure in DynamoDB especially if your use case has update scenario.
TableName : 'tablename',
Key : { id: id},
ReturnValues : 'ALL_NEW',
UpdateExpression : 'set someitem['+`index`+'].somevalue = :reply_content',
ExpressionAttributeValues : { ':reply_content' : updateddata }
For updating nested array element need to fing out array index . Then you can update nested array element in dynamo db.

Meteor query by array attribute

Suppose I have my collection as follows:
{
"_id" : "PipdmEzTMAziXjWBn",
"text" : "jkhkj",
"createdAt" : ISODate("2017-02-27T13:43:22.856Z"),
"hashtags" : [ "fwXJcu5CpKcYZpQ3v", "DCS4eLNiS7NjipiDQ" ] }
The important thing here is that i store id's of hashtags as an array.
Suppose I have a hashtag id as my input, how do I find all records that contain that id?
EDIT:
With the help of #zim, I did this:
Feeds.find({hashtags: {$in: "DCS4eLNiS7NjipiDQ"}});
But now it says:
Exception while simulating the effect of invoking 'feeds.findByHashtag' Error: $in needs an array
for a top-level query, you can use $in. e.g.
let hashtagId = ['abc123'];
collection.find({hashtags: {$in: hashtagId}});

MongoDB and .NET: Filter on specific field in child array using FilterDefinition

I have a class which creates a list of filters from an input.
The filters are found by calling a function for each filter like this:
public void created_after(string date)
{
DateTime convertedDate = Convert.ToDateTime(date);
filters.Add(Builders<User>.Filter.Gte(x => x.Created, convertedDate));
}
Now i need to segment on a field on a child array on the user.
In this case, i just need to know if any alert has a Created value higher than a given date.
My data looks like this:
{
"DisplayName" : "PestisanRadu",
"Alerts" : [
{
"UserId" : ObjectId("577a26a12b365917c4d67dd5"),
"Created" : ISODate("2016-10-05T09:17:44.382+0000")
},
{
"UserId" : ObjectId("577a26a82b365917c4d68009"),
"Created" : ISODate("2016-10-05T18:44:45.743+0000")
}
],
"Created" : ISODate("2016-10-05T09:17:43.423+0000")
}
For the class to work, i need to keep the FilterDefinition type.
Use other same method which take FieldDefinition as parameter, in your case would be
filters.Add(Builders<User>.Filter.Gt("Alerts.Created", convertedDate));
Note that the string "Alerts.Created" is FieldDefinition

Datatype mismatch in Jaydata

I am managing offline db using jaydata + sqlite.
In jaydata nvarchar(20) is not a valid datatype so I need to use 'String' as datatype.
Online db is using datatype as nvarchar(20).
Now,my question is:
Is it possible to map both offline db and online db because 'nvarchar(20)' is datatype on online db and 'string' is datatype on offline db' ??
This is what I am supposed to create in my online db:
This is what I have created in my offline db with jaydata+sqlite:
// Table definition of user
$data.Entity.extend("user", {
'UserId' : { key:true,type:'int',nullable:false,required:true },
'Username' : { type:'string',nullable:false,required:true },
'Password' : { type:'string',nullable:false,required:true},
'UserType' : { type:'string',nullable:false,required:true},
'CreatorId' : { type:'int',nullable:false,required:true},
'CreatedAt' : { type:'datetime',nullable:true},
'ModifierId' : { type:'int',nullable:true},
'ModidiedAt' : { type:'datetime',nullable:true},
'IsDeleted' : { type:'boolean',nullable:false,required:true}
});
disclaimer: I am one of the creators of JayData
You can use your online schema to create an offline database. NVarchar(20) is not even an OData type - so I guess it will just be 'Edm.String' with a maxLength=20 modifier. Edm.String is however mapped to the type "String" in JavaScript.
Sharing your schema definition would also help me giving better answers.
EDITED
The following schema definition would work with an online endpoint (provided with odata or REST) and would also create the same table definition locally.
$data.Entity.extend("user", {
'userid' : { key:true,type:'string', maxLength:216,required:true },
'username' : { type:'string',maxLength:216, required:true },
'password' : { type:'string',maxLength:64,required:true},
....
});
Is this what you needed? Please note that in JS every string is nvarchar since javascript is UTF8 internally.

Resources