Querying nested values with Q4 2015 version 1.1.3 - firebase

Based on Tom's answer to this question, I'm getting the following error when trying to query a nested field on a collection.
I'm trying to get a single user object with personalityExams.id.extId = 24232. I'd like to know the best way to query this, and how to form an index rule for it.
Here is what the user looks like in firebase
{
"users": {
"93306b91-d5ba-4e06-838c-0ab85fd58783": {
"createdOn" : 1451495976870,
"email" : "iyad.bitar#gmail.com",
"firstName" : "Iyad",
"lastModifiedOn" : 1451590413654,
"lastName" : "Bitar",
"mobile" : "34234333",
"provider" : "password",
"userType" : "JS",
"personalityExams": [
{
"extId": "24232",
"skills": ["teaching"]
}
]
}
In my javascript, I'm trying to get it like so:
(new Firebase("https://hirely-dev.firebaseio.com/users")).orderByValue("personalityExams/extId").equalTo("24232").on("value", function(snapshot) {user1 = snapshot.val();});
Error:
Uncaught Error: Query.orderByValue failed: Was called with 1 argument. Expects none.
at Error (native)
at x (http://localhost:7200/lib/firebase/firebase.js:20:1074)
at U.g.Fg (http://localhost:7200/lib/firebase/firebase.js:233:343)
at <anonymous>:2:59
at Object.InjectedScript._evaluateOn (<anonymous>:875:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:808:34)
at Object.InjectedScript.evaluate (<anonymous>:664:21)

Related

dynamodb with help of np dynamodb or np vogels, throwing Invalid schema content error

I am trying to create a schema using node package dynamodb/vogels but it is throwing Invalid schema content. I used the below sample schema
var Account = vogels.define('Account', {
hashKey : 'email',
// add the timestamp attributes (updatedAt, createdAt)
timestamps : true,
schema : {
email : Joi.string().email(),
name : Joi.string(),
age : Joi.number(),
roles : vogels.types.stringSet(),
settings : {
nickname : Joi.string(),
acceptedTerms : Joi.boolean().default(false)
}
}
});
error details
Uncaught Error: Invalid schema content:
node_modules/vogels/node_modules/hoek/lib/index.js:731
Debugger attached.
Waiting for the debugger to disconnect...
c:\my-workspace\dynamoDB\node_modules\vogels\node_modules\hoek\lib\index.js:731
throw new Error(msgs.join(' ') || 'Unknown error');
^
Error: Invalid schema content:
at Object.exports.assert (c:\my-workspace\dynamoDB\node_modules\vogels\node_modules\hoek\lib\index.js:731:11)
at Object.exports.schema (c:\my-workspace\dynamoDB\node_modules\vogels\node_modules\joi\lib\cast.js:65:10)
at internals.Object.keys (c:\my-workspace\dynamoDB\node_modules\vogels\node_modules\joi\lib\object.js:269:25)
at Object.exports.schema (c:\my-workspace\dynamoDB\node_modules\vogels\node_modules\joi\lib\cast.js:46:33)
at internals.Object.keys (c:\my-workspace\dynamoDB\node_modules\vogels\node_modules\joi\lib\object.js:269:25)
at Object.exports.schema (c:\my-workspace\dynamoDB\node_modules\vogels\node_modules\joi\lib\cast.js:46:33)
at internals.Object.keys (c:\my-workspace\dynamoDB\node_modules\vogels\node_modules\joi\lib\object.js:269:25)
at c:\my-workspace\dynamoDB\node_modules\vogels\lib\schema.js:105:74
at internals.Object.internals.Any._validateWithOptions (c:\my-workspace\dynamoDB\node_modules\vogels\node_modules\joi\lib\any.js:460:16)
at module.exports.internals.Any.root.validate (c:\my-workspace\dynamoDB\node_modules\vogels\node_modules\joi\lib\index.js:102:23)
Process exited with code 1
Has anyone come across this or know how to create the schema using node page dynamodb/vogels?

AWS AppSync - DeleteItem doesn't execute response mapping template

When attempting to delete an item using the following request mapping:
{
"version" : "2017-02-28",
"operation" : "DeleteItem",
"key" : {
"id": { "S" : "$ctx.args.id"},
"sortKey" : { "S" : "$ctx.args.sortKey"}
}
}
If the item exists it will process the result through the response template, however when the item does not exist the response template is never run.
Response template:
#set($ctx.result.status = "SUCCESS")
#set($ctx.result.message = "This was a success!")
$utils.toJson($ctx.result)
I am aware that when an item does not exist in Dynamo it will perform no action but I would expect that it would still process through the template.
Is there anything I am missing or is it impossible for AppSync to processed a DeleteItem request through the response mapping when the document does not exist?
This the expected execution behavior for the version of the template you are using (2017-02-28).
You can switch your request mapping template version to 2018-05-29 and your response mapping template will be executed, with the following characteristics:
If the datasource invocation result is null, the response mapping template is executed.
If the datasource invocation yields an error, it is now up to you to handle the error. The invocation error is accessible using $ctx.error.
The response mapping template evaluated result will always be placed inside the GraphQL response data block. You can also raise or append an error using $util.error() and $util.appendError() respectively.
More info https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-changelog.html#aws-appsync-resolver-mapping-template-version-2018-05-29
So for your example:
{
"version" : "2018-05-29", ## Note the new version
"operation" : "DeleteItem",
"key" : {
"id": { "S" : "$ctx.args.id"},
"sortKey" : { "S" : "$ctx.args.sortKey"}
}
}
and response template
#if ( $ctx.error )
$util.error($ctx.error.message, $ctx.error.type)
#end
#set($ctx.result.status = "SUCCESS")
#set($ctx.result.message = "This was a success!")
$utils.toJson($ctx.result)

MongoDB query returns "Error: Invalid JSON object" on valid query

Issue
I have probably mucked up the syntax for the JSON query, but cannot for the life of me see where. Trying to create data.frame overviewData grouped on four values with an extra COUNT-feature.
Code
overviewData <- M_CONNECTION$aggregate('[
{
"$group" : {
"_id" : {
"Hotel_Name" : "$Hotel_Name",
"lat" : "$lat",
"lng" : "$lng",
"Average_Score" : "$Average_Score"
},
"COUNT(Hotel_Name)" : {
"$sum" : NumberInt(1)
}
}
},
{
"$project" : {
"Hotel_Name" : "$_id.Hotel_Name",
"lat" : "$_id.lat",
"lng" : "$_id.lng",
"Average_Score" : "$_id.Average_Score",
"COUNT(Hotel_Name)" : "$COUNT(Hotel_Name)",
"_id" : NumberInt(0)
}
}
]',
options = '{"allowDiskUse" : true}'
)
I have quotes around all of my queries, as well as around the options. Still getting an "Invalid JSON object"-error message.
Though I'm not sure, I guess this line is causing you trouble ->
"$sum" : NumberInt(1)
Instead of this, please try this ->
"$sum" : 1
Kindly try this and let me know if this helped.

Mongodb count the number of a field with certain condition

This is the data I have in the database:
{
"_id" : ObjectId("5bf84d5eb6655873af59ead6"),
"game" : 1.0,
"action" : {
"actionType" : "GameStart",
"actionNumber" : 0.0,
"Player1" : {
"user" : 2.0,
"name" : "Kevin"
},
"Player2" : {
"user" : 4.0,
"name" : "Sue"
}
}
}
The question is to report the total number of started games. I tried this code db.hw6.count({'action.actionType': "GameStart" }), and got an error. I have no idea how I did it wrong. can anyone help?
Please use itcount() instead of count to solve the error.
It is because you are using Azure CosmosDB rather than MongoDB. count() will work for mongodb but in your case you should use itcount() which is CosmosDB implementation.
Please refer mongodb documentation. https://docs.mongodb.com/manual/reference/method/cursor.itcount/
Below error will be resolved after using itcount()******
Error: count failed: { "_t" : "OKMongoResponse", "ok" : 0, "code" : 13, "errmsg" : "Cannot execute command ExecuteJavaScript using PrimaryReadonlyMasterKey", "$err" : "Cannot execute command ExecuteJavaScript using PrimaryReadonlyMasterKey" } : _getErrorWithCode#src/mongo/shell/utils.js:25:13 DBQuery.prototype.count#src/mongo/shell/query.js:383:11 DBCollection.prototype.count#src/mongo/shell/collection.js:1700:12 #(shell):

Decent data structure for Firebase messaging?

I'm trying to get started with Firebase and I just want to make sure that this data structure is optimized for Firebase.
The conversation object/tree/whatever looks like this:
conversations: {
"-JRHTHaKuITFIhnj02kE": {
user_one_id: "054bd9ea-5e05-442b-a03d-4ff8e763030b",
user_two_id: "0b1b89b7-2580-4d39-ae6e-22ba6773e004",
user_one_name: "Christina",
user_two_name: "Conor",
user_one_typing: false,
user_two_typing: false,
last_message_text: "Hey girl, what are you doing?",
last_message_type: "TEXT",
last_message_date: 0
}
}
and the messages object looks like so:
messages: {
"-JRHTHaKuITFIhnj02kE": {
conversation: "-JRHTHaKuITFIhnj02kE",
sender: "054bd9ea-5e05-442b-a03d-4ff8e763030b",
message: "Hey girl, what are you doing?",
message_type: "TEXT",
message_date: 0
}
}
Is storing the name relative to the user in the conversation object needed, or can I easily look up the name of the user by the users UID on the fly? Other than the name question, is this good? I don't want to get started with a really bad data structure.
Note: Yes, i know the UID for the conversation & message are the same, I got tired of making up variables.
I usually model the data that I need to show in a single screen in a single location in the database. That makes it possible to retrieve that data with a single read/listener.
Following that train of thought it makes sense to keep the user name in the conversation node. In fact, I usually keep the username in each message node too. The latter prevents the need for a lookup, although in this case I might be expanding the data model a bit far for the sake of keep the code as simple as possible.
For the naming of the chat: if this is a fairly standard chat app, then user may expect to have a persistent 1:1 chat with each other, so that every time you and I chat, we end up in the same room. A good approach for accomplishing that in the data model, can be found in this answer: Best way to manage Chat channels in Firebase
I don't think you structured it right. You should bare in mind "What if" complete analysis.
Though, I would recommend structuring it this way (I made it up for fun, not really tested in-terms of performance when getting a huge traffic. but you can always do denormalization to increase performance when needed):
{
"conversation-messages" : {
"--JpntMPN_iPC3pKDUX9Z" : {
"-Jpnjg_7eom7pMG6LDe1" : {
"message" : "hey! Who are you?",
"timestamp" : 1432165992987,
"type" : "text",
"userId" : "user:-Jpnjcdp6YXM0auS1BAT"
},
"-JpnjibdwWpf1k-zS3SD" : {
"message" : "Arya Stark. You?",
"timestamp" : 1432166001453,
"type" : "text",
"userId" : "user:-OuJffgdYY0jshTFD"
},
"-JpnkqRjkz5oT9sTrKYU" : {
"message" : "no one. a man has no name.",
"timestamp" : 1432166295571,
"type" : "text",
"userId" : "user:-Jpnjcdp6YXM0auS1BAT"
}
}
},
"conversations-metadata" : { // to show the conversation list from all users for each user
"-JpntMPN_iPC3pKDUX9Z" : {
"id": "-JpntMPN_iPC3pKDUX9Z",
"date":995043959933,
"lastMsg": "no one. a man has no name.",
"messages_id": "-JpntMPN_iPC3pKDUX9Z"
}
},
"users" : {
"user:-Jpnjcdp6YXM0auS1BAT" : {
"id" : "user:-Jpnjcdp6YXM0auS1BAT",
"name" : "many-faced foo",
"ProfileImg" : "...."
"conversations":{
"user:-Yabba_Dabba_Doo" : {
"conversation_id": "-JpntMPN_iPC3pKDUX9Z",
"read" : false
}
}
},
"user:-Yabba_Dabba_Doo" : {
"id" : "user:-Yabba_Dabba_Doo",
"name" : "Arya Stark",
"ProfileImg" : "...."
"conversations":{
"user:-Jpnjcdp6YXM0auS1BAT" : {
"conversation_id": "-JpntMPN_iPC3pKDUX9Z",
"read" : true
}
}
}
}
}

Resources