association queries in feathers js - node.js

How do we create or add the sample paramter below to be a valida query params. Ive tried something below but it is error . does someone has an idea ? thanks.
#URL
http://localhost:3030/peopltable?$or[0][addedBy.person.name][$like]=HELLO&targetId=865
#what i tried
if (query ) {
query = {
targetId: query.targetId,
$or[0][addedBy.person.name][$like] = "Hello"
};
}

The same information from the URL should be called like:
if (query ) {
query = {
targetId: query.targetId,
$or: [ { 'addedBy.person.name': { $like: "Hello" } } ]
};
}

Related

Specify response type for inline fragment graphql in js file

Here, inline fragment graphql is used. I'm not able to write the return type in the js file.
Graphql:
query MyQuery {
samples(dataset: "", view: "") {
edges {
node {
... on ImageSample {
id
}
... on PointCloudSample {
id
}
}
}
}
}
JS file: this raises a syntax error:
const SAMPLE_DATA = {
edges: {
node: {
... on ImageSample {
id
sample
}
... on PointCloudSample {
id
}
}
}
};
I've also tried with node: {id} but didn't help
Cannot query field 'id' on type 'SampleItem'. Did you mean to use an inline fragment on 'Sample', 'ImageSample', 'PointCloudSample', or 'VideoSample'?
Calling the GraphQL query like this:
const gqlQuery = jsonToGraphQLQuery({
query: {
samples: {
__args: {
...data,
},
...SAMPLE_DATA
}
}
}, { pretty: true });
Can anyone help me how we need to write the SAMPLE_DATA response type?
I authored the GraphQL API. The below is a perfectly valid query as of v0.18.0.
query {
samples(dataset: "datasetName", view: []) {
edges {
node {
... on ImageSample {
id
}
... on PointCloudSample {
id
}
}
}
}
}
I believe you just need to follow the json-to-graphql-query instructions for querying with multiple inline fragments.
const SAMPLE_DATA = {
edges: {
node: {
__on: [
{ __typeName: "ImageSample", id: true },
{ __typeName: "PointCloudSample", id: true }
]
}
}
};

Find and update child of child in MongoDB

Let's take an exemple :
listCollection = {
id_list:"111",
child_list:[{
id_list:"222",
child_list:[{
id_list:"333",
child_list:[{
// and it can be more
}]
}]
}]
}
As you see, it is always the same object inside the same object. The initial object is :
var list = {
id_list: "string",
child_list:[]
};
Using mongoDB, I would like to Find, Update or Push list anywhere I want inside the collection, only by knowing the id_list.
This is the most far I was :
db.collection("listCollection").findOneAndUpdate({
"child_list.id_list": "listId"
}, {
"$push": {
"child_list.$.child_list": newList
}
}, function(err, success) {
if (err) {
console.log(err);
} else {
console.log(success);
}
})
It works fine for the first level of child, but not more.
I am pretty new to Node JS + mongoDB, can you help me a little bit with this ? Thank you :)
I have found my way through this. Not a really beautiful one, but a working one.
Let's retake my exemple :
listCollection = {
child_list:[{
child_list:[{
child_list:[
]
}]
}]
}
I want to add a new child_list at the end of the hierarchy. At the end, I want my collection to look like that :
listCollection = {
child_list:[{
child_list:[{
child_list:[{
child_list:[
]
}]
}]
}]
}
Yeah, it is kind of a dumb exemple but it is a simple one :)
So here is the object I want to insert :
var theNewChildList= {
child_list:[]
};
Then, I will need a kind of "route" of where I want to put theNewChildList.
Here, I want to push it at listCollection.child_list[0].child_list[0].child_list
So I just use that string :
var myRoute = "child_list.0.child_list.0.child_list";
Then, I prepare your mongoDB query like that :
var query = {};
query[myRoute] = theNewChildList;
and finally, I do the query :
db.collection("myCollectionOfList").findOneAndUpdate(
{}, // or any condition to find listCollection
{"$push": query},
function(err, success) {
if (err) {
console.log(err);
} else {
console.log(success);
}
})
And it works.
Note that you have many ways to prepare the var myRoute dynamically.
I you have other ideas, please share :)

dynamically add fields to find() mongodb

Hi i am working with mongoDB , i am trying to crate a dynamic object and pass as an argument to the find()
my object is like this
Library
var search = {};
if(data.orderId) {
search["_id"] = { $in: data.orderId.split(",") } ;
}if(data.path) {
search["stops.districtId"] = data.path;
}if(data.special) {
search["special.option"] = { $in: data.special.split(",") } ;
}if(data.userInfo) {
search["UserId"] = data.userInfo;
}
then i will pass my search objet to the query like this
Model
var col = db.collection( CustomerOrderModel.collection() );
col.find(
{
serviceType:data.serviceType,
**search**
}
).skip(data.skip).limit(data.limit).sort(data.sort).toArray(function(err, res) {
if (err) {
reject( err );
} else {
resolve( res );
}
});
the problem here is when i console.log my search object i can see
'special.option': { '$in': [ 'ROUND_TRIP' ] } }
my $in is wrapped with quotes . so my query doesn't work .
if i directly type "special.option": { $in: [ 'ROUND_TRIP' ] } } in my query it is working correctly .
i m trying to built this search object because i have multiple fields to search with complex logic . i don't want to do these in my
model , so i wil create the search object in my library .
is there any possible ways to this , thanks in advance .
You should make the search object part of the query by adding the extra filters into the search object. As you are currently doing
col.find({
serviceType:data.serviceType,
search
})
this is interprated as
col.find({
serviceType:data.serviceType,
{ 'special.option': { '$in': [ 'ROUND_TRIP' ] } }
})
You should be able to add the serviceType filter to your existing search object using the square bracket notation as follows:
search["serviceType"] = data.serviceType;
then you can pass that object in your query:
var col = db.collection( CustomerOrderModel.collection() );
search["serviceType"] = data.serviceType;
col.find(search)
.skip(data.skip)
.limit(data.limit)
.sort(data.sort)
.toArray(function(err, res) {
if (err) { reject( err ); }
else { resolve( res ); }
});
That is not the problem.
console.log({ "special.option": { $in: [ 'ROUND_TRIP' ] } });
gives
{ 'special.option': { '$in': [ 'ROUND_TRIP' ] } }
so this is correct.
In your code you just write **search** in the most critical part, but try this:
search["serviceType"] = data.serviceType;
col.find( search )

$pull/$pop on embedded array mongodb

I have a simple collection with the following schema
{
name:"John",
brands:[
{
name:"some",
email:"asdf#some.com"
},
{
name:"blah"
email:"blah#blah.com"
}
]
}
i'm using the following query to remove the embedded object inside my brands array field:
var args = {
'query':{name:"John",brands.email:"asdf#some.com"}
,update:{
'$pull':{
'brands.$.email:"asdf#some.com"
}
}
}
i'm using nodejs driver for mongodb and when i run the above using following:
collectionName.findAndModify(args,function(req,res){
})
I get the following error:
MongoError: Cannot apply $pull/$pullAll modifier to non-array
I guess i'm doing correct but still getting this error. Any help appreciated.
Your $pull is targeting email which isn't an array. If you're trying to remove the matching element, you can do it like this:
var args = {
query: {name: "John"},
update: {
'$pull': {
brands: {email: "asdf#some.com"}
}
}
}
or if you're trying to remove the email field, use $unset instead:
var args = {
query: {name: "John", "brands.email": "asdf#some.com"},
update: {
'$unset': {
'brands.$.email': 1
}
}
}

How to use 'BatchGetItem' for the NodeJS AWS-SDK for DynamoDB

I am trying to get items out of a DynamoDB table using the Node JS AWS-SDK. The function getItem is working fine but BatchGetItem is harder to use.
I use the official documentation:
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/Client.html#batchGetItem-property
I am looking for examples on how to use this function correctly, but I can't find any. The code I wrote is:
var params = {
"RequestItems" : {
"Keys" : [
{"HashKeyElement" : { "N" : "1000" } },
{"HashKeyElement" : { "N" : "1001" } }
]
}
}
db.client.batchGetItem(params, function(err, data) {
console.log('error: '+ err);
console.log(jsDump.parse(data));
});
I get a SerializationException: Start of list found where not expected error but as far as my NodeJS and JSON expertise goes, my syntax is correct. But it's confusing:
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/API_BatchGetItems.html
In that syntax example, you have to provide the table name.
I used the dynamo db client version... after an hour of research I managed to make it work...
var params = {
RequestItems: { // map of TableName to list of Key to get from each table
Music: {
Keys: [ // a list of primary key value maps
{
Artist: 'No One You Know',
SongTitle:'Call Me Today'
// ... more key attributes, if the primary key is hash/range
},
// ... more keys to get from this table ...
],
AttributesToGet: [ // option (attributes to retrieve from this table)
'AlbumTitle',
// ... more attribute names ...
],
ConsistentRead: false, // optional (true | false)
},
// ... more tables and keys ...
},
ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
};
docClient.batchGet(params, function(err, data) {
if (err) ppJson(err); // an error occurred
else ppJson(data); // successful response
});
I feel your pain... AWS documentation is confusing at best. I think it's caused by aging infrastructure and bad technical writing. The nodejs and JSON syntax used by the SDK reminds me of XML structure.
Anyway, I managed to get the BatchGetItem to work after a whole hour. The params should look like below:
{
"RequestItems": {
"<TableName>": {
"Keys": [
{"<HashKeyName>": {"<type>":"<hash key value>"}},
{"<HashKeyName>": {"<type>":"<hash key value>"}},
{"<HashKeyName>": {"<type>":"<hash key value>"}}
]
}
}
}
I believe that you're missing table name. You want this:
var params = {
"RequestItems" : {
"TableName": {
"Keys" : [
{"HashKeyElement" : { "N" : "1000" } },
{"HashKeyElement" : { "N" : "1001" } }
]
}
}
}
I tried all of the solutions here and none of them worked for me, which likely means the NodeJS library has gotten an update. Referencing their better written docs, you should be able to make a request like so:
var params = {
RequestItems: {
'Table-1': {
Keys: [
{
HashKey: 'haskey',
NumberRangeKey: 1
}
]
},
'Table-2': {
Keys: [
{ foo: 'bar' },
]
}
}
};
var docClient = new AWS.DynamoDB.DocumentClient();
docClient.batchGet(params, function(err, data) {
if (err) console.log(err);
else console.log(data);
});
Specifically, providing the type is no longer necessary.
Try this:
db.client.batchGetItem(
{"RequestItems":{
"TableName":{
"Keys":[
{"HashKeyElement" : {"N":"1000"}},
{"HashKeyElement" : {"N":"1001"}}
]
}
}
}, function(err, result){ //handle error and result here });
In your case, the correct answer should be:
var params = {
"RequestItems": {
"<table_name>": {
"Keys": [
{"HashKeyElement" : { "N" : "1000" } },
{"HashKeyElement" : { "N" : "1001" } }
]
}
}
}
Try this, it's untested though:
var params = {
TableName: "tableName",
RequestItems : {
Keys : [
{
HashKeyElement : { N : "1000" }
},
{
HashKeyElement : { N : "1001" }
}
]
}
}

Resources