I would like to check if the array "devices" which is in the document "SOjTxEjMwb71isiiPnR8" contains the value "AE0EBAFA-1560-404B-A3DA". I just want to know this specific array in the specific document. Because it can be that the same value in the same array occurs in another document. How do I have to do this in Swift? Thanks a lot for your help.
You're looking for the array-contains operations, which in Swift looks like this:
devicesRefRef
.whereField("devices", arrayContains: "AE0EBAFA-1560-404B-A3DA")
You can then get the results once or listen for realtime updates. You'll note that all links I provide are to the relevant sections of the documentation, so I recommend spending some time there.
Related
This is how my data look like.
I'm able to use FieldValue.increment(1) to update individual fields but is there a way to use FieldValue.increment(1) for specific individual elements in the array? Thanks in advance!
I tried using the following code:
firestore.collection('test').doc('I1raMaJArb1sWXWqQErE')
.update({
'rating.0': FieldValue.increment(1),
});
But the whole rating became empty as seen
You can't use FieldValue.increment() if the field is part of the array. The value of the field inside the array is fixed. The best way to update or edit the field that part of an array is to read the entire document, modify the data in memory and update the field back into the document.
I have this this collection in Firestore where each document has 'count' property, and it is int of 1. I would want to have a function that adds that count property of 1 to total property somewhere in Firebase, I presume it would be another collection that would just calculate the sum as they are added 1 by 1. I'm at a bit of a loss how I would go around doing that.
The collection is displayed in a regular Stream and each element is in a List Tile. So what I'm after is to get a function that would be called somewhere that would add that '1' to the let's say collection called 'Total' and it would calculate whenever that function is executed and add that 1 to total. Also, how would I display that 'Total' collection somewhere else on another page as a string perhaps?
I attached a general image how my Firestore looks like just to give you guys a general picture.
I don't think code is necessary here, since as I said, it is just a regular List Tile and Stream of the 'Names' collection, but let me know if you need more context. Thank you!
In Cloud Firestore in order to increase an integer property, you would need first to iterate through the documents, by reading stored data and subsequently perform an update to change the value of this specific field in the corresponding "Total" collection.
To display the collection using flutter, you can take a look at StreamBuilder which can listen to the document changes and display appropriately, I also found this Medium article which might help.
I want to get a single Observable from a collection, but I want to get it from a different field that is not the id. It is possible?
I do not want to do a query and limit to 1. I need to get a Single Observable not an array Observable.
Schema:
Code:
this.afs.doc<Credit>('credits/uid/'+ uid).valueChanges();
Error:
Invalid document reference. Document references must have an even number of segments, but credits/uid/d1Zt8sozYqb6H27lhoJgF1Gx2Cc2 has 3
I am not sure if I understand correctly, but I guess that you want to get document with particular uid field value, not using document id.
This particular error is related with common feature of Firestore, that every document has to be in collection not in document. So path values for documents, (nested as well) are always checked, if the segments (devided by /) number is even ex. collection1/doc1/collection2/doc2/collection3/doc3
As results in your code we have 3 segments (like credits/uid/<uid_value>) so this is the error.
I am not very familiar with angularFire2 itself, but I have done it in JS. The approach is normally to query collection and than use method on the results, which in classic JS returns Query object on which the same methods can be used as on CollectionReference (which extends 'Query' btw - reference 1).
Combining this approach with those references: querying and collection I propose following solution:
this.afs.collection('credits', ref => ref.where('uid', '==', <uid_value>)).valueChanges()
If uid_value will be unique you should get your doc.
Unfortunately I do not have any playground to test the solution so please let me know how it works - or if there will be any additional errors.
I hope it will help! Good Luck!
I have a collection users in firestore where one of the fields is "contact_person".
Then I have an array arrNames {'Jim', 'Danny', 'Rachel'} in my frontend and I want to get all users that have either of these names in their "contact_person" field.
Something like where("contact_person" IN arrNames)
How could I do that?
This is currently not possible within the Firestore API. You will need to do a separate get for each document, unless the names happen to be in a single contiguous range.
Also see:
FireStore Where In Query
Google Firestore - how to get document by multiple ids in one round trip?
It is now possible (from 11/2019) https://firebase.googleblog.com/2019/11/cloud-firestore-now-supports-in-queries.html but keep in mind that the array size can't be greater than 10.
db.collection("projects").where("status", "in", ["public", "unlisted", "secret"]);
I have two "documents" that I inserted into my MongoDB database.
questionsList.insert({question: "When was the War of 1812", answer: "1812", answers: ["1811", "1812", "1813", "1814"]})
questionsList.insert({question: "What year did the US land on the moon?", answer: "1969", answers: ["1969", "1970", "1971", "1972"]})
I simply want to access the answer value from the second document. I have been reading the documentation and it doesn't seem to work. I can retrieve the answer value from the first document without issue: var str = questionsList.findOne({}, {question: 1}).answer; I presume that since I am using findOne I can't find any other matches. The problem is that I can't seem to pull up the second document and its corresponding answer. I have tried many different ways:
questionList.find({}, {answer: 1})
questionList.find({answer: 1})
questionList.find({}).answer
My ultimate goal is to compare this answer with one of the click one answers from choices What am I missing?
If I understood your scenario correctly, you are trying to retrieve a document based on the document index (Which is not the right thing to do, since MongoDB does not store documents in specific order).
The reason why findOne works is, because it just returns the first document in your collection.
What I believe you should do instead is retrieve the answer based on the question. Something like:
db.questionsList.find({question:"What year did the US land on the moon?"},{answer:1})
Update:
In the case of meteor.js
questionsList.find({question:"What year did the US land on the moon?"}).fetch()[0].answer
The reason whey we need to give [0] is fetch() returns an array of objects. (Since there can be multiple documents with same key)
The final step is:
questionsList.find({"question": "You are human"}, {"answer": 1}).fetch()[0].answer
We are treating it as any other object (i.e. the first within a list of objects and using dot-notation)