someone help me deleting element inside object array
I want to delete a object inside an array in mongodb this is my code but it isn't deleting
code attached above
Related
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?
Suppose this scenario.
I have a collection where I store products, this products have a property called "props" and this is an array of objects, that have properties like measure, weight.
The problem comes when I'm trying to insert new products. I'm receiving an array of objects that will be inserted, but each element of the array could have a property called "propsReference", and If it's there, it would be an ID that refers to the object that's currently inserted and have some props
So, if it has the propsReference property with an ID, I want to copy the properties of this object to the object I'm going to insert
The only solution I can think of is doing a foreach to the first array, if it has the propsReference do a query, get the props and insert it into this element of the array as a new property of the object.
But I'm having a lot of problems with doing queries inside a for loop, there's any way in mongoose or something to avoid that?
Thanks in advance
i've been googling around about how to add an object into an array in firestore, and found the arrayUnion() able to add an object into firestore array, but it only add the object into last index of array, but how to add it into first index of array?
//add "greater_virginia" into last index of array
washingtonRef.update({
regions: firebase.firestore.FieldValue.arrayUnion("greater_virginia")
});
//how to add "greater_virginia" into first index of array?
its basically same as arrayUnion but instead of add it into last index, i want to add it into first index of array.
If Firestore arrays behave anything like realtime-database arrays, then they don't actually exist. As far as I know, they are store as maps, like:
{
0: "first element",
2: "second and so on",
}
You can probably see how an unshift would be a big transformation. In fact, firestore doesn't let you do this, saying "...in order to avoid some of the issues that can arise in a multi-user environment, you'll be adding them with more of a set-like functionality".
With that in mind, this problem is usually solved at the application level by fetching the array, mutating it as needed, then setting the whole thing.
Bit of further reading https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html
PS: be careful with the arrayUnion operator, because it actually performs a add to set
Firestore doesn't offer any way to modify items of array fields by index. arrayUnion will only ever append to the end of the array if the element doesn't already exist.
If you want to modify an array by index, you will have to read the document, modify the array in memory to appear how you want, then write the modified array back to the document.
How do I check if an element of an array belongs to another array in Mongoose.
For example, I have a document:
{"_id":"","username":"","password":"","email":"","image":"1-81-14.svg","dob":"","marital_status":"","phone":"","categories":["catering_services","legal_services","computer_programmers","rentals","fashion_designers","education"]}
and another document of the same collection:
{"_id":"","username":"","password":"","email":"","image":"1-81-14.svg","dob":"","marital_status":"","phone":"","categories":["computer_programmers","rentals","fashion_designers","education", "music"]}
How do I check if at least an element of the array in the categories node in the first document belongs to the array in the second document
I want to update an item in my application so I need an id that Firebase creates:
https://db.firebaseio.com/userid/books/-L6_e2KsseGzwM8a0YUv
The problem is that Firebase returns an object of objects, so I returned Object.values(data) and now I don't have that id which I could maybe access if Firebase returns classic array..
you will get the key of the object by object.key. and for value you can do object.val()
For multiple object you can try Object.keys() and Object.values()
Is this you wanted? I can be more specific if you can post the block of code.