Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore - Does the dot notation work with set()?

I am trying to run this code:

function updateUserLimitations(userId, limitations, batchOrTransaction = undefined) {
  const userLimitationsRef = firestore
    .collection("users")
    .doc(userId)
    .collection("limitations")
    .doc("userLimitations");

  if (batchOrTransaction) {
    return batchOrTransaction.set(
      userLimitationsRef,
      limitations,
      { merge: true }
    );
  }

  return userLimitationsRef.set(limitations, { merge: true });
}

updateUserLimitations(userId, { "messages.totalMessages": admin.firestore.FieldValue.increment(1) });

But...

Instead of getting this doc data in my db:

{ // Doc data
   messages: {
      initialDate: timestamp, // Date of the first message (I need to preserve it),
      totalMessages: 20,
   },
}

I am getting:

{
   ...other doc data...,
   messages.totalMessages: 20,
}

I need the set with the merge option, because I am updating and creating if the doc doesn't exist....

Any ideas? Am I doing something wrong here?

like image 643
Victor Molina Avatar asked Sep 15 '26 11:09

Victor Molina


1 Answers

You can use an object for "setting" nested data:

{
  messages: {
    totalMessages: 20,
  }
}

The "dot" notation is for applying updates to nested data.

like image 84
Elliot Hesp Avatar answered Sep 18 '26 00:09

Elliot Hesp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!