Why is firebase not recognizing this query? [duplicate] - firebase

This question already has answers here:
How to use array-contains operator with an array of objects in Firestore?
(2 answers)
Closed last year.
This is my query:
ref.where(
"tags",
"array-contains-any",
[{ tagTitle: 'webdev' }],
)
I know that this query works because if I try it with a 'simple' query, it works.
For example:
ref.where(
"type",
"==",
"post",
)
However, is there anything else that I have to change than the query, because right now, this is the only thing that I have changed?
This is my firestore collection:
As you can see, there is a tags field, and I want to select the tagTitle field.
I want to show all the documents with that query, this has worked with the second example. (the type == post) one.
But right now I don't get an error, but it also doesn't show in my website.

Only if you pass both the id and the tagTitle will it work.
ref.where('tags', 'array-contains-any', [{ id: 480170, tagTitle: 'webdev' }]);

Related

firebase how to query through array of objects [duplicate]

This question already has answers here:
How to use array-contains operator with an array of objects in Firestore?
(2 answers)
Firestore - query where filter of object in array field
(1 answer)
Closed 10 days ago.
I need to write a query for Firestore which will filter records based on id in the array, so I have a collection of "assignments" and each assignment has records more or less like this:
{
"name" :"abc",
"status": "accepted",
"assignedAt": "timestamp",
"createdAt": "timestamp",
"recipients": [
{
id: "123",
name: "recipient1"
},
{
id: "456",
name: "recipient2"
}
]
}
I wish to write a query that will check if user.id exists as an id in the recipients array, except that I also filter out some statuses but this part works, so far I have something like this:
assignmentCollection
// .where('recipients', 'array-contains', user!.id)
.where('status', 'not-in', recipientAssignmentSkippableStatuses)
.orderBy('status', 'desc')
.orderBy('assignedAt', 'desc')
.orderBy('createdAt', 'asc')
I tried to use something called "array-contains" but:
a) it didn't work
b) it cause error that cannot be used together with "not-in"
EDIT: I found out that array-contains doesn't work as I need to pass the whole object for that, thing is that at this moment I know only the ID of the user so I need to find some workaround
I found out that array-contains doesn't work as I need to pass the whole object for that, the thing is that at this moment I know only the ID of the user so I need to find some workaround.
Yes, in order to return documents that contain a particular object in the array, you need to pass the entire object as an argument, not only a single field.
If you need to filter based on the user ID, then you should consider creating an additional array that only contains user IDs. Then you'll be able to call:
.where('userIds', 'array-contains', user!.id)

Create item if not found and return old one if existing in dynamodb, in one call

In DynamoDb, is it possible to do a conditional put and return the old item if there already was a matching item?
I would like something like the following to create the row if the user does not already exist, and if it does exist, I want it to return the old item (with whatever name was there before). I.e., insert item if new, else just read existing item.
await documentClient.put({
TableName: 'table',
Item: {
userId: 'user0',
name: 'smith',
},
ConditionExpression: 'attribute_not_exists(userId)',
});
Is this possible to do in one call?
Sort of.
If you use UpdateItem it will create or update the item. It only updates the fields that have changed however, so if your fields have not changed, then no update will be made. It will also create the item if you do not have it.
Using Conditional Items for this will return an ErrorCode if the condition fails ConditionalCheckFailedException - which would require an additional operation.
(I don't use the javascript SDK that much, more in python, so I'm not sure this is the correct version/page but here is some documentation:
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#updateItem-property )

How to query documents where contains an array and the value of the array is ["val1", "val2"] Firestore

How can I get a collection where the query should be applicable to an array inside the document.
Document example: I would like to know how to query the document where the brands are fiat and seat
{
"name":"test 1",
"brands":[
{
"brand":{
"id":1,
"name":"Fiat",
"slug":"fiat",
"image":null,
"year_end":null,
"year_start":null
},
"released_at":"2018-10-26"
},
{
"brand":{
"id":2,
"name":"Seat",
"slug":"seat",
"image":null,
"year_end":null,
"year_start":null
},
"released_at":"2018-10-26"
},
{
"brand":{
"id":3,
"name":"Mercedes",
"slug":"mercedes",
"image":null,
"year_end":null,
"year_start":null
},
"released_at":"2018-10-26"
},
{
"brand":{
"id":4,
"name":"Yamaha",
"slug":"yamaha",
"image":null,
"year_end":null,
"year_start":null
},
"released_at":"2018-10-26"
}
]
}
I have tried something like:
.collection("motors")
.where("brands.slug", "array-contains-any", ["fiat", "seat"])
but this is not working I cannot figure out by the documentation how to get this.
When using the array-contains-any operator, you can check the values of your array against the value of a property of type String and not an array. There is currently no way you can use array-contains-any operator on an array. There are two options, one would be to create two separate fields and create two separate queries or, been only a document, you can get the entire document and filter the data on the client.
Edit:
What #FrankvanPuffelen has commented is correct, I made some research and I found that we can check against any type and even complex types, not just against strings, as mentioned before. The key to solving this issue is to match the entire object, meaning all properties of that object and not just a partial match, for example, one of three properties.
What you are trying to achieve is not working with your current database structure because your slug property exists in an object that is nested within the actual object that exists in your array. A possible solution might also be to duplicate some data and add only the desired values into an array and use the array-contains-any operator on this new creatded array.

How to query nested objects in firestore [duplicate]

This question already has answers here:
Firestore - Nested query
(2 answers)
Closed 2 years ago.
I want to store data in following format:
{
"chatName": "Football",
"chatMembers":
[
{
"userId": "nSWnbKwL6GW9fqIQKREZENTdVyq2",
"name": "Niklas"
},
{
"userId": "V3QONGrVegQBnnINYHzXtnG1kXu1",
"name": "Timo"
},
]
}
My goal is to get all chats, where the signed in user with a userId is in the chatMembers list. If the userId of the signed in user is not in the chatMembers property, then that chat should be ignored. Is this possible?
If this is not possible, how can i achive this with subcollections?
My development language is dart, but you can also post solutions in other languages.
My current attempt is this, but this is not working:
_firestore.collection(collectionName).where("chatMembers.userId", isEqualTo: userId).snapshots()
Since August 2018 there is the new array_contains operator which allows filtering based on array values. The doc is here: https://firebase.google.com/docs/firestore/query-data/queries#array_membership
It works very well with arrays of string. However, I think it is not possible to query for a specific property of an object stored in the array. One workaround is to query for the entire object, as follows (in Javascript). Of course this may not be feasible in every situation....
var db = firebase.firestore();
var query = db.collection('chatDocs').where("chatMembers", "array-contains", { userId: "xyz", userName: "abc" });
Renaud Tarnec's, which complete, doesn't work in every case. Situations where only one or not all of the fields are known won't return the expected documents. However, by restructuring the data in the document, a query can be made to work where only one field is known, like a user identifier (uid).
Here's the data structure inside one of the document:
{
"members": {
"user4": {
"active": true,
"userName": "King Edward III",
"avatar": "www.photos.gov/animeGirl5.png"
},
"user7": {
"active": true,
"userName": "Dave K.",
"avatar": "www.photos.gov/gunsAmericanFlag.png"
}
}
Here's the query:
uid = 'user4';
collectionQuery = collectionReference.where(`members.${uid}.active`,"==", true);
In this example, "user4" represents a user who may or may not be in a group. This will return all documents in the collection where the uid "user4" is an active member. This works by only needing to know the UID of the member and works without needing to know their name or avatar uri ahead of time.

How to return a subdocument from a document based on parameters in Minimongo?

Short question
Is there a way to filter documents client side?
Long question
So I have user accounts in a current Meteor JS project that store subdocuments, such as emails and comics.
_id: "jGZgBRqPRGFakcQRS",
comics:[
{_id: "z4fq6QWKfQiX4G5gb",
appearanceName: "Paradigm's Los",
coverart: "sampleurl",
heroName: "deadpool",
publisher: "marvel"
volNo: "-1"},
{_id: "kvQLtT5nMdqhsxBRp",
appearanceName: "Test",
coverart: "sampleurl2",
heroName: "deadpool",
publisher: "marvel",
volNo: "1"}],
emails: [{address: "email#email.com",
verified: false}],
username: "test"
I also have a different collection named "Volumes", that passes the _id, appearanceName, coverArt, heroName, publisher, and volNo to the comics array on click. I currently have an {{#each}} displaying the documents from Volume. I want to display client side a certain link if a certain comic exists in the "comics" array AND if the _id of the item in the comics array matches the Volumes _id and a different link if it doesn't meet both requirements. Since aggregation doesn't work, I'm completely stuck. Thanks ahead of time.
I didn't find easy way how to filter data in subdocuments, so I always create separate collection. Then you need to join collections, you can read how to do it in this article: http://www.meteor.hromnik.com/blog/joins-in-meteorjs-and-mongodb
If you want to just find data in subdocument you can do it:
Meteor.users.findOne({
'comics._id': yourComicsId
});
It will find user document with given comics id.

Resources