How to efficiently query object with large field array? - javascript

So basically I have an object schema that has an array field that will hold ids for objects inside a different collection. This array field has the potential to have thousands of ids inside of it. I have been omitting this field using .select(["-fieldName"]); in my queries up till now but I need to include it in my query if I wanna add on to it.
I would assume querying an object with such a large field is costly in performance so therefore my question is how can I efficiently query such an object?
I would like to just omit this field in my query but then I can't add ids into it.

If you just want to add an item to the array field you can use the $push operator.
collection.updateOne(query, { $push: { arrayField: value } });

Related

Fetch field with dynamic key where date greater than [duplicate]

This question already has answers here:
How to use array-contains operator with an array of objects in Firestore?
(2 answers)
Firestore to query by an array's field value
(3 answers)
Query firestore array of objects
(2 answers)
Closed last month.
After reading a bunch of threads regarding trying to perform queries on fields with dynamic keys in a document, I have two questions.
Is it possible to perform the following "query" on my data model: "Get field where createdDate is equal or greater than todays date"?
Should I change the data model?
I have documents (test.com in the below image) that have fields where the key is dynamic (0, 1, 2, 3 and so on). Each field is a "map", which (correct me if Im wrong) contains an object. Each object have a "createdAt". I want to query all fields on "createdAt" and return all fields where "createdAt" is equal to or greater than todays date. So in my image, as of today it is the 12 of january, the field with the key "1" should be returned.
My query below doesn't work due to I don't "reach down to each field", would each document have just color, createdAt and domainName flattened It would have worked.
const domainsQuery = query(
collectionGroup(db, "domains"),
where("releaseAt", ">=", new Date())
);
It's not possible to fetch only some fields of a document in firebase. You always fetch all fields of a document. You can filter returned documents but not the fields of documents. You should probably adjust your model.
I recommend just creating an additional subcollection (name can be arbitrary) and making those dynamic keys into ids of subdocuments instead.
Is it possible to perform the following "query" on my data model: "Get field where createdDate is equal or greater than today's date"?
No, there is not. You cannot filter documents based on a single field property that exists inside an object, which is contained in an array. What you are looking for cannot be achieved using partial data.
If you need to filter based only on a particular field, then you should consider duplicating the data on which you want to perform the filtering and adding it to a separate array. In this way, you can query the collection using the array-contains operator.
Should I change the data model?
A possible solution would be to get all those objects out of the array and add them as separate documents inside a sub-collection. In that way, you can simply perform the desired query.

elastic-builder: how to retrieve all unique value of one specific field

I am completely new using elastic search with current project(elastic-builder is used) and documentation of elastic-builder is bit confusing.
I need to retrieve all unique values of one specific field, say creator of tasks stored on server, how should I compose the query to achieve this?
You can use the terms aggregation to get all the unique values sorted by count from a field. If you use the default mapping for your index, your creator field should have a subfield creator.keyword that you can use for the terms aggregation.

How can I update an object inside of an array in firestore?

I would like to update the completed property of an object in an array in Firestore, but I have no idea how to reach that specific element in the array. The image will show the structure.
I have come up this far but don't know how to choose, for example, item 1 in the array. I was thinking of using its ID (it has an id property) but don't know how to get there.
const businessRef = db.collection('approvedBusinesses').doc(businessId)
try {
businessRef.update({
[`bookings.${currentDate} ????? `]: true // what to add after currentDate?
})
By the way, this is how the array was created (and how other objects are pushed to it)
const bookingObj = {
carro: 'PASSA_CARRO',
completed: false,
userId: userObject.uid,
}
businessRef.update({
[`bookings.${currentDate}`]: firebase.firestore.FieldValue.arrayUnion(bookingObj),
})
Firestore does not have an operation that allows you to update an existing item in an array by its index.
To update an existing item in the array, you will need to:
Read the entire document into your application.
Modify the item in the array in your application code.
Write back the entire array to the document.
I'm pretty sure this has been asked before, so let me see if there's an answer with an example.
Also see:
How to remove an array element according to an especific key number?
Simple task list ordering - how to save it to Firebase Firestore?
How to update only a single value in an array
How to update an "array of objects" with Firestore?

underscore .find() multiples with same key value

I am currently trying to filter an object via POST from a form the form is made of checkboxes and as a result I need to be able to search an array of objects for multiple values on the same key.
My POST looks similar to this,
{
tax_year: ['2016/17', '2017/18'],
status : ['completed'],
user : [1,4,78]
}
How would I go about search an array of objects and returning all the objects that have a matching key and value? I know I can do a single find with underscore like so,
var result = _.where(historyData, {tax_year: "2016/17"});
but I have no clue as to how to search for multiple matching keys and values?
It seems that you're matching on both of the values in an array value (regardless of whether there are more array values or not). If that's true, you might have to use the _.filter() method, where you can provide whatever logic you need in the 'predicate'. Alternatively, you could add a unique identifier on the server side to each record and match on that.

Use array or JSON to keep track of unique list

I have an object in Parse .com database that can receive thumbs up votes. I want to keep track of which users have provided a thumbs up to prevent the same user doing it again. I am thinking of adding a field to my object called "voters" that will hold the list of voting users.
Should this be an array of usernames or a JSON with keys as the usernames?
Why would I use an array if i could use a json map and do O(1) lookup of the names on the keys?
ie object.voters[uername] = undefined then no votes?
Use Array if you need just the name of the voter .
Use Json if you need more info than just the name .
Why is that if u need just the name just making an array with the voter names first will be easy second it will be faster t process .
but if you need something like
voter={"name":"foo","age":"25"}
as you see we can use the powerof json to save multiple info about one user and anyway each voter must be within a single array so you can iterate over them
UPDATE---
to find the index of a name in an array just use array.indexOf("the name") what this will return is the index number of the name if the name doesnt exist it will return just -1 so a simple if else can handle this
UPDATE---
And here i found a performance test to compare which is faster to lookup whitin an array or an object and seems arrays still win this.
And here is another source for reading

Categories

Resources