Where filter with HasAndBelongsToMany relation in Loopback - loopback

first of all I am sorry if this has been asked 1st, I searched for this and got few links but wasn't helpful.
I am working on simple chat application created in Angular 4 which has Customer entity and Conversation entity and Message. In The customer => hasAndBelongsToMany => Conversation with MongoDb Connector.
In customer.json
"relations": {
"conversations": {
"type": "hasAndBelongsToMany",
"model": "Conversation"
}
}
In conversation.json
"relations": {
"customers": {
"type": "hasAndBelongsToMany",
"model": "Customer"
}
}
This creates table customerconversation which contains conversationId and customerId
Now basically I want to find conversation for two customer Ids, So I tried following filters but it doesn't seem to work and always returns empty array even though there is conversation having these two customer ids.
let filter = {
where: {
and: [
{
customers: {
inq: [
this.customerId // = 59bb981f35fcc941e8ba64e4
]
}
},
{
customers: {
inq: [
this.authService.getCurrentId() // = 59bb98c735fcc941e8ba68ff
]
}
}
]
}
};
and another without and operator
let filter = {
where: {
customers: {
inq: [
this.customerId, // 59bb981f35fcc941e8ba64e4
this.authService.getCurrentId() // 59bb98c735fcc941e8ba68ff
]
}
}
};
These both returns customer with mentioned Id but with 0 conversations. I know I can get all conversations of specific customer by using include filter for conversations but still those would need to be filtered as I want single conversation of this customer with another specific customer.

You can get the information by grabbing the customer first and then using the relations and include to get the data you need.
Example is based on the assumption that you are writing this using typescript & loopback-sdk-builder on an angular application.
this.customerApi
.getConversations(this.authService.getCurrentId(), {
where: {
customerId: this.customerId
})
.subscribe(result => {
// your logic here
})

Related

How to query an object in an array embedded in mongodb?

I am currently working on an application that takes control of Projects, which have Meetings and that these meetings have Participants.
I want to consult a Participant by his nomina field.
Structure for a project document object:
{
"id":"5c1b0616a0441f27f022bfdc",
"name":"Project Test",
"area":"Area",
"date":"2019-01-01",
"meetings":[
{
"id":"5c1b073d445707834699ce97",
"objetive":"Objetive",
"fecha":"2019-01-01",
"participants":[
{
"nomina":1,
"name":"Person 1",
"role":"Rol1",
"area":"area1",
"signature":null
},
{
"nomina":2,
"name":"Person 2",
"role":"rol 2",
"area":"área 2",
"signature":null
}
]
}
]
}
Expected behavior
I want to consult a Participant by nomina field knowing the id of the Project and also knowing the id of the Meeting.
Expected output
Having:
id Project = 5c1b0616a0441f27f022bfdc
id Meeting = 5c1b073d445707834699ce97
nomina Participant = 1
It's expected that the query will return me:
{
"nomina":1,
"name":"Person 1",
"role":"Rol1",
"area":"area1",
"signature":null
}
For not so huge number of meetings in every document if you want to get the exact document stated, you can do this pipeline, it is straight forward:
db.collection.aggregate(
[
{
$match: {
id:"5c1b0616a0441f27f022bfdc"
}
}, {
$unwind: {
path : "$meetings"
}
},
{
$unwind: {
path : "$meetings.participants"
}
},
{
$match: {
"meetings.id":"5c1b073d445707834699ce97",
"meetings.participants.nomina":1
}
},
{
$replaceRoot: {
newRoot: "$meetings.participants"
}
}
]);
If you would have over thousands of elements in meetings then I'd suggest adding another match to meetings or grouping meetings and project IDs.
But if you just want to get the document containing what you want it is just a simple find query:
db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 });

Posting to Consecutive Commands to Solr - Data Missing from 1st Call

I am sending the following commands with
my $url = "http://xxxxx/solr/inventory/update?commitWithin=1000";
I am using perl to send to a solr setup on another server.
Please excuse the formatting. I really did try.
Thanks
Mike
RESULTING DATA - The data from the first command is not here. All subsequent calls are.
{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"q":"*:*",
"fq":"id:3-159682",
"_":"1529984183431"
}
},
"response":{
"numFound":1,
"start":0,
"docs":[
{
"checklist_id":249746,
"brand_s":"Pinnacle",
"featured":"",
"sf_set_sort":"Baseball1992Pinnacle",
"sf_set_sort_s":"Baseball1992Pinnacle",
"sport_s":"Baseball",
"cardnumber":"308",
"issue_s":"",
"id":"3-159682",
"year_s":"1992",
"team":"Los Angeles Dodgers",
"set_name_s":"",
"has_image":1,
"amazon_sku":"159682",
"amazon_sync":1,
"sf_id":378827,
"sf_ending_time":2222222222,
"sf_sort_id":199230875,
"sf_listing_type":"buy",
"shopify_id":"1302493397094",
"_version_":1604345060355211264
}
]
}
}
COMMANDS AND RESPONSES
[
{
"inv_location":"",
"ean":"",
"site_id":"3",
"category_id":[
"1",
"55",
"2162220",
"2715086",
"306",
"2352370",
"2413461"
],
"cp_id":"159682",
"isbn":"",
"id":"3-159682",
"consigner":"",
"upc_code":"0",
"quantity":"1",
"created_date":"2018-06-26T10:17:55Z",
"mpn":"",
"description":"",
"inv_num":"",
"cp_listing_type":"1",
"price":"0.69",
"title":"1992 Pinnacle #308 Darryl Strawberry NM-MT ",
"live_status":""
}
]
Success: {
"responseHeader":{
"status":0,
"QTime":1
}
}
[
{
"checklist_id":"249746",
"brand_s":"Pinnacle",
"featured":"",
"sf_set_sort":"Baseball1992Pinnacle",
"sport_s":"Baseball",
"cardnumber":"308",
"issue_s":"",
"id":"3-159682",
"year_s":"1992",
"team":"Los Angeles Dodgers",
"set_name_s":""
}
]
Success: {
"responseHeader":{
"status":0,
"QTime":1
}
}
[
{
"has_image":{
"set":"1"
},
"id":"3-159682"
}
]
Success: {
"responseHeader":{
"status":0,
"QTime":1
}
}
[
{
"amazon_sku":{
"set":"159682"
},
"amazon_sync":{
"set":"1"
},
"id":"3-159682"
}
]
Success: {
"responseHeader":{
"status":0,
"QTime":1
}
}
[
{
"sf_id":{
"set":"378827"
},
"sf_ending_time":{
"set":"2222222222"
},
"sf_sort_id":{
"set":"199230875"
},
"id":"3-159682",
"sf_listing_type":{
"set":"buy"
}
}
]
Success: {
"responseHeader":{
"status":0,
"QTime":1
}
}
[
{
"id":"3-159682",
"shopify_id":{
"set":"1302493397094"
}
}
]
Success: {
"responseHeader":{
"status":0,
"QTime":1
}
}
The two documents you submit have the same id. The second document overwrites the first one, since the id has to uniquely identify a document. If the id doesn't do that, change which field is defined as a uniqueKey, or use a UUID generator to get a new id each time the document is submitted. The latter will cause issues if you're trying to do updates without having the new uuid readily available, tho.
Another solution would be to prefix the id with the document type, or (depending on your use case) merge it into a single document before indexing.
The answer to my problem was that the 1st and 2nd commands were full updates, and the rest were partial updates (using "set"
I change the second command to a format like this
[{"checklist_id":{"set":"249725"},"brand_s":{"set":"Pinnacle"},"featured":{"set":""},"sf_set_sort":{"set":"Baseball1992Pinnacle"},"sport_s":{"set":"Baseball"},"cardnumber":{"set":"287"},"issue_s":{"set":""},"id":"3-159694","year_s":{"set":"1992"},"team":{"set":"Milwaukee Brewers"},"set_name_s":{"set":""}}]
And all was right with the code, no lionger overwriting the first query.
Maybe this will help someone else!
Thanks
Mike

Graphcool (Graphql): how to make exact filter in array of Id

I have chat model
type Chat #model {
id: ID! #isUnique
name: String
messages: [Message!]! #relation(name: "ChatMessages")
users: [User!]! #relation(name: "ChatUser")
createdAt: DateTime!
}
And I've created a chat with two people ["123", "234"] and all was ok. But when I want to create a new group chat with three people where two of them already have a chat with themselves ["123", "234", "345"]
I make a query for check is this chat exist with three people
query {
allChats(filter:{
users_every: {
id_in: ["123", "234", "345"]
}
}
){
id
users{
id
}
}
}
and I got response that this chat already exist, but here I have just two users not all of them
{
"data": {
"allChats": [
{
"id": "cjgxuub2351uj0187qeil548m",
"users": [
{
"id": "123"
},
{
"id": "234"
}
]
}
]
}
}
The every suffix in users_every makes sure it matches all chats that have only users with ids from the id_in array. That's why you're getting chats with fewer users, because subsets match.
The solution:
query {
allChats(
filter: {
AND: [
{ users_some: { id: "123" } },
{ users_some: { id: "234" } },
{ users_some: { id: "345" } },
{ users_every: { id_not_in: ["678", "910", ...] } }
]
}
) {
id
users {
id
}
}
}
The users_some filters make sure to select chats that have at least all those 3 users attached. The users_every - id_not_in is needed to exclude the chats with those three plus any other users.
In all honesty this seems a bit convoluted to me, and probably kinda unfeasible for an app with a large number of users. I'd love for graph.cool to implement some easier solution in the api.
As an alternative, you could just omit the users_every filter, and then, in your client app, pick from the results only the chat that has exactly 3 users.

Querying grandchild properties in Firebase

I try to come up with a Firebase realtime DB structure for an online store.
The store should have a collection of products, each product can belong to one or more categories. Is it possible to construct a query to get all products in the computers category with a single HTTP request (assuming I use Firebase REST API)? Here is a sample piece of data:
{
"products": {
"-KaXxv2xD9WaIqHMsHYM": {
"title": "Item 1",
"categories": {
"electronics": true,
"computers": true
}
},
"-KaXyvdw5gmuBmGi5unb": {
"title": "Item 2",
"categories": {
"electronics": true
}
},
"-KaXyyyzmP9Y6askhLdx": {
"title": "Item 3",
"categories": {
"computers": true
}
}
}
}
I was also trying to use arrays for categories but looks like arrays support is very limited in Firebase and they should be avoided.
UPDATE:
This query works:
GET /products.json?orderBy="categories/computers"&equalTo=true
But it requires an index for every single category:
{
"rules": {
"products": {
".indexOn": ["categories/computers", "categories/electronics"]
}
}
}
You should have an additional categories node which have products list. That would make easier and efficient access to products for a specific category.
Similar aproach is used at Firebse sample. See code
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + "/" + key, postValues);
They have save same data at posts and user-posts nodes.

Sorting by document values in couchbase and scala

I am using couchbase and I have a document (product) that looks like:
{
"id": "5fe281c3-81b6-4eb5-96a1-331ff3b37c2c",
"defaultName": "default name",
"defaultDescription": "default description",
"references": {
"configuratorId": "1",
"seekId": "1",
"hsId": "1",
"fpId": "1"
},
"tenantProducts": {
"2": {
"adminRank": 1,
"systemRank": 15,
"categories": [
"3"
]
}
},
"docType": "product"
}
I wish to get all products (this json is product) that belong to certain category, So i've created the following view:
function (doc, meta) {
if(doc.docType == "product")
{
for (var tenant in doc.tenantProducts) {
var categories = doc.tenantProducts[tenant].categories
// emit(categories, doc);
for(i=0;i<categories.length;i++)
{
emit([tenant, categories[i]], doc);
}
}
}
}
So i can run the view with keys like:
[["tenantId", "Category1"]] //Can also have: [["tenant1", "Category1"],["tenant1", "Category2"] ]
My problem is that i receive the document, but i wish to sort the documents by their admin rank and system rank, these are 2 fields that exists in the "value".
I understand that the only solution would be to add those fields to my key, determine that my key would be from now:
[["tenantId", "Category1", "systemRank", "adminRank"]]
And after i get documents, i need to sort by the 3rd and 4th parameters of the key ?
I just want to make sure i understand this right.
Thanks