How to add field to all documents in database mongoose [duplicate] - javascript

This question already has answers here:
Add new field to every document in a MongoDB collection
(7 answers)
Closed 1 year ago.
I am trying to add a field to all documents in the database, but I am not sure how to do that. Here is my database
const db = client.schema
How would I update all documents and add a field called tiks (number) in it?

db.yourCollection.update(
{},
{ $set: {"newField": 1} },
false,
true
)
Parameters
Collection to update since you want all {}
Field to set
Upsert - only add when it is not there hence false
Multi - update multiple documents if matches to query hence true

Related

How can i update an item in an array on firebase [duplicate]

This question already has answers here:
Firestore Update single item in an array field
(2 answers)
How can I update an object inside of an array in firestore?
(1 answer)
Closed 6 months ago.
I'm trying to make a button that can edit an item within a documents array in firebase. Basically, the functionality I'm building is: you click an EDIT button, and it changes a tag into an tag where you would make your edit/update to the array item. Then you would click a DONE button and the update to the array would happen. For some reason, I can only find documentation on firebase for updating a document, and not an item inside the array. I've tried using arrayUnion but it only adds a new item to the array. I'm trying to update the info[0].tags in the screenshot below. Please let me know if I'm missing information to help make this work.
My issue seems to be I can't do: info[i]: arrayUnion({tags: tags}) so I'm not sure how to tell the updateDoc function which item in the info array I need to update.
Here's the code:
const editCard = async (tags, i) => {
const fieldRef = doc(db, "labels", activeLabels);
await updateDoc(fieldRef, {
info[i]: arrayUnion({ tags: tags }),
});
console.log(tags);
getLabels();
findLabel(activeLabels);
toast.success("Tags Updated!");
};
[![firebase document and array][1]][1]
[1]: https://i.stack.imgur.com/PDJcc.png

firestore updateDoc removes other fields in array [duplicate]

This question already has answers here:
How to update array elements in Firestore with Android?
(2 answers)
How to update an array item in Firestore
(2 answers)
Firestore Update single item in an array field
(2 answers)
Closed 11 months ago.
async function editBackEnd() {
const entryRef = doc(db, "accounts", user.uid)
await updateDoc(entryRef, {
"entry.0.payment": "League"
})
}
I'm trying to update certain fields in an array in firestore. I know there is no direct way to do so but with this code i am able to access the field but it removes all the other entries/fields.
How do I make sure it only updates the field that i want. in this cause im just trying to update payment of index 0
Im having issues uploading pics to stackoverlow so here's a couple url
https://imgur.com/a/1hqNgsn firestore database before updateDoc
https://imgur.com/a/Egzofvz firestore database after updateDoc

How do I query a firebase collection by the 'not in' parameter with more than 10 values? [duplicate]

This question already has answers here:
Firebase Firestore query an array of more than 10 elements
(5 answers)
How do I query a firebase collection by the 'in' paramater with more than 10 values?
(1 answer)
Closed 1 year ago.
I have a contact list:
[
"abc"
"def"
]
and chat records that have the following format
from :"dpasha52"
to: "jbabu"
text: "some message"
timestamp :1 September 2021 at 17:57:02 UTC+5:30
I need to query firebase collection such that
===========>not in takes contact list
this.angf.collection('Chats', ref => ref.where('from',not in, ["","",""]).where('to','==',this.userinfo.name))
.valueChanges();
the only problem is not in only allows 10 entries.
is there any way to include more than 10 entries?
the only problem is not in only allows 10 entries
Yes, that is correct, you can only perform a query up to 10 entries.
How do I query a firebase collection by the 'not in' parameter with more than 10 values?
If you have more than 10 entries, then you should consider retrieving the entries in batches, and then process the entries that are coming from each separate query either sequentially or in parallel.
Is there any way to include more than 10 entries?
No, currently there isn't.

Mongoose field not required but unique [duplicate]

This question already has answers here:
mongoDB/mongoose: unique if not null
(4 answers)
Closed 5 years ago.
I have an email field set as unique, but it is not required.
The problem is that if the user does not enter anything Mongoose puts "null" in it. This causes duplicates because every user that does not enter the email field will have "null" assigned to it.
What is the standard practice to avoid this?
Thanks
Use a sparse unique index
If a document does not have a value for a field, the index entry for
that item will be null in any index that includes it. Thus, in many
situations you will want to combine the unique constraint with the
sparse option. Sparse indexes skip over any document that is missing
the indexed field, rather than storing null for the index entry.
db.collection.createIndex( { a: 1, b: 1 }, { unique: true, sparse: true } )
More information: https://docs.mongodb.com/v3.0/tutorial/create-a-unique-index/

findOneAndUpdate not returning updated document in MongoDb [duplicate]

This question already has answers here:
Mongoose findByIdAndUpdate not returning correct model
(2 answers)
Closed 3 years ago.
I have the following code to find and update a field of an embedded document in MongoDb.
var fieldToIncrement = { $inc: {} };
fieldToIncrement.$inc['options_counter.'+field[0]] = 1;
console.log(fieldToIncrement);
db.collection('polls').findOneAndUpdate(
{_id: user_id},
fieldToIncrement,
{
returnNewDocument: true
}, function(err, doc){
if(err){console.log("Error updating the document")};
if(doc){
callback(doc);
}
}
)
My data structure looks like so
_id:"57e0149dcb0b20156878598d"
options:"osgn, srgounsrog, snourv"
options_counter:Object
0:0
1:0
2:0
The variable field in the code is an array that contains one element which references which field of the options_counter object I want to update. However, the document is returned without any update and no error is returned either. I tried changing the
fieldToIncrement.$inc['options_counter.'+field[0]] = 1;
to
fieldToIncrement.$inc['opt_counter.'+field[0]] = 1;
and it still doesn't return an error. This has to mean I am not
implementing the code correctly because opt_counter is not a field in
my document, therefore it SHOULD return an error.
It won't return an error when you call it on wrong field which doesn't exist, $inc would create a field with that name with value as 0 and then increment that by 1.
After the update call if your field value is not changing (even with a wrong field) maybe you need to call the query with find({_id: user_id}) and see if it is returning the data or not, sometime we store _id as ObjectId and query it by String value and it doesn't pick the document due to type mismatch.

Categories