Mongoose find if an array element is in another array - javascript

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

Related

can not delete elements inside object array using $pull

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

Firebase(firestore) update data with array

What I should do to change the status value in the document specified in the desired collection groups.
You can't update a single property on a specific item in the array. You will have to:
Read the entire document into you application code.
Get the current value of the data array from the document.
Update item 0 in the data array in the application code.
Write the entire data array back to the database.

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?

How to check if document exist with particular field in mongodb

I have an array of users and I have one collection named users. Now I want to find the documents which has user_id equals to users from Array. And if that user id doesn't exist then no action taken. I'll loop through array to get one user id at a time. Now my concern is how to find the document with that particular user_id in MongoDB
Output I want:
Array a= [1,2,3,4,5]
document with user_id=1
document with user_id=2
document with user_id=3 like that
Thanks for help!!!
You can use $in operator. For more information https://docs.mongodb.com/manual/reference/operator/query/in/
For example
db.collection.find({user_id:{$in:[1,2,3,4,5]}})

Get all unselected nodes in dynatree

I have implemented dynatree. I can get all selected nodes simply by:
$("#tree").dynatree("getSelectedNodes");
How can I similarly get all unselected nodes?
$('#tree') will give you an array of all the nodes. And your code here will give you an array of selected nodes. You can loop through the main tree nodes array and generate a new array by checking if the node in a particular iteration does not match with selected nodes array.

Categories

Resources