Firestore set doc with merge option true or update doc - node.js

I have to update a single field in an existed Firestore document.
However, I don't know if there is some difference between using
admin.firestore().doc('doc_id').set({name:'Bill'},{ merge: true })
or
admin.firestore().doc('doc_id').update({name:'Bill'})
If the document is:
{
name: Bart,
age: 18
}
Should .update(...) just update the field and not remove the field "age" in this case?

There is no difference between those two options for existing documents.
The difference between them is only evident for documents that don't exist. set() with merge will create the document if it does't exist, and update() will fail if it doesn't exist.

Related

How to query field exist some document in firebase

I using firebase, nodejs and i have a question how to query field exist some document.
Example :
Collections : users => document : A with field {
....
is_correct: true
}
document : B with field {
.....
}
In my above example , i have two document in collection users. On document A i have field is_correct: true and on document B field is_correct not exist.
My collection users about 15.000 document and it have 50 document contain field is_correct: true
When i write code look like :
await firestore.collection('users').where('is_correct', '==', true).get();
it can get correct me 50 document. But i don't understand it can using index on field is_correct or not ? And it can query best performance ? I understand firebase can't get document if field undefined. It impart in case ? Please help ? Thanks you
For a simple query like
firestore.collection('users').where('is_correct', '==', true)
you don't need to configure any index, Firestore does it automatically.
As you mentioned, only documents where the given field exists can match the query.
And this is the case also for Not-equal (!=) and not-in queries: they exclude documents where the given field does not exist, as explained in the documentation.
Also, note that a field exists when it's set to any value, including an empty string (""), null, and NaN (not a number).
So, in conclusion, if you want to query for documents where is_correct is not true, you need to create this field in these documents with a value different than true.

Firebase Cloudfirestore update elements in a collections

i'm using firebase functions with nodejs and cloudfirestore to build an API, now i have a problem, i have a collection an inside the collection an array of objects.
My goal is to update only a specific element without read the whole object and then push it again.
This is how my "order's collection" looks like:
"someRestaurantId":[{id:1,desc:"test",val:3000},{id:2,desc:"test",val:4000},{id:4,desc:"test",val:5000}]
My goal es only update id 2.
This is what i been trying:
Get the full list based on
const document = db.collection('orders').doc(req.params.restId);
Iterate and find the correct element, modify it and update it again.
await document.update(fullobject)
The problem is that i need something more easy to handle since there could be thousand of element inside that array.
Looking at described issue, I think you should use subcollection (sometimes called nested collection) for that. However from the description it is hard to tell what do you have now.
Firestore collection structure is always like this:
collection -> document -> fields and nestedCollections
than nestedCollection has the same structure, so you can nest whole structure how many times you want, but always collection contain only documents and each document contain only fields and collection.
In presented collection description array is directly in the collection or directly in document which both are impossible.
Anyway my idea is to structure it following way:
Collection orders containing docs of various restIDs
every docs (ex. "someRestaurantId") contain subcollection with name ex. "RestOrders"
in the subcollection will add documents with ids like (1,2,3 etc.)
every doc in subcollection will have fields desc, val
Collection Orders:
Document RestID_1:
Subcolection RestOrders:
Document 1: {val: 3000, desc: test}
Document 2: {val: 3000, desc: test}
...
Document RestID_2:
Subcolection RestOrders:
Document 1: {val: 3000, desc: test}
Document 2: {val: 3000, desc: test}
...
And now to update value of "RestID_1", order "id 2" you can use:
db.collection('Orders')
.doc('RestID_1')
.collection('RestOrders')
.doc('2')
.update( {val: 4000} )
In above solution you avoid reading and updating big array. You can update only one filed of one document in nested sub-collection.

Update same field in different documents with different value at once with MongoDB

I didn't found a question similar to mine and i'm not sure it's possible. I have several documents, each document is a person, for example :
{
"name" = "Paul",
"score" = 105
}
{
"name" = "John",
"score" = 98
}
Before the update i have a dict (in python) with the name and the new score {"Paul": 107, "John": 92}. How do i update the score in all the documents from the dict in one request ?
You can not update multiple documents with different conditions in a single query. You can refer MongoDB update doc. MongoDB introduced a new parameter as multi but it has a different meaning. update query with param { multi: true } will update multiple documents only which will match the same condition which we set in query part.
Optionally you can update multiple documents with loop through your query. This feature is still missing in MongoDB so we are also doing such thing in the same way.

How to Match the string from collection after lookup with many collection using mongodb

Here My query
model.db.aggregate([{$lookup: {from: 'orders', localField: 'prod_id',
foreignField: '_id', as: 'Col'}},{$match:{$text:
{$search:'Sale'}}}).exec((err,data) => {console.log(data);}]);
but error showing "$match with $text is only allowed as the first pipeline !!"
I just want to lookup many collection then only I have to match'Search' in all the data what we joined(lookup) before.
mongoDb version: 4.0
Anybody have an idea ? need Help !
Thanks !!
These all are my Example collections:
collection 1 ->
organization ={'_id':ObjectId("5b110a7b84a0442030a1e9cf"),'Org_name':'dhoni institute','Estd':'1945'}
collection 2 -> players= {'_id':ObjectId("45110a7b84a3542030a1e9cf"),'_name':'Ms dhoni','Org_id' = ObjectId("5b110a7b84a0442030a1e9cf") }
I am searching the text string 'dhoni' in Db..then I want all the documents which contains word 'dhoni' from these collections.
How to do ?
db.players.aggregate([{$match:{$text:{$search:'dhoni'}}},
{
$lookup{from:'organization',localField:'_id',foreignField:'Org_id',as:'org'}
}
]).exec((err,data) => {}
this is my code It only matches the string from 'players' collection .I need matched 'players' collection documents as well as 'Organization' collection documents.
I cannot create new collection because after loopup data may be a huge data so
I cannot inserting new large data every time search
How to match the string after lookup ?
As explained in official mongodb documentation,
$text performs a text search on the content of the fields indexed with a text index.
But indexes are reachable only by the first stage of an aggregation query. That's the reason of your error message.
The only way i see for you to use $text/$search is to perform your aggregation (without match stage), adding an $out stage to output to a new collection, create a text index on that collection, and perform your find query with $text/$search criteria.
Hope it helps.

I want to do a solr search to see if a dynamic field exists or not

I want to do a solr search to see if a dynamic field exists or not.
Example:
Doc 1 {
Id: 111
Name: good
Tag_100_is: lsdkl
}
Doc 1 {
Id: 2
Name: not good
}
I want a query to retrieve doc 1.
Thank you in advance.
To query for whether a field exists or not, use field:[* TO *]. So in this case, you should get the document you want by using the query Tag_100_is:[* TO *].
If you want to get the document without the field, you'll have to invert the query (we start with : which are "all documents", then remove the documents that have the field):
q=*:* -Tag_100_is:[* TO *]

Resources