Javascript 是否可以为firestore中的阵列设置限制?

Javascript 是否可以为firestore中的阵列设置限制?,javascript,firebase,react-native,google-cloud-firestore,Javascript,Firebase,React Native,Google Cloud Firestore,我计划在firestore中创建一个最多只有5个元素的数组,如下所示 数组a=[1,2,3,4,5] 然后添加元素6,它将如下所示 数组a=[2,3,4,5,6]此云函数(可在此处找到:)在实时数据库中实现您想要的功能: 'use strict'; const functions = require('firebase-functions'); // Max number of lines of the chat history. const MAX_LOG_COUNT = 5; // R

我计划在firestore中创建一个最多只有5个元素的数组,如下所示 数组a=[1,2,3,4,5]

然后添加元素6,它将如下所示

数组a=[2,3,4,5,6]

此云函数(可在此处找到:)在实时数据库中实现您想要的功能:

'use strict';

const functions = require('firebase-functions');

// Max number of lines of the chat history.
const MAX_LOG_COUNT = 5;

// Removes siblings of the node that element that triggered the function if there are more than MAX_LOG_COUNT.
// In this example we'll keep the max number of chat message history to MAX_LOG_COUNT.
exports.truncate = functions.database.ref('/chat').onWrite((change) => {
  const parentRef = change.after.ref;
  const snapshot = change.after

  if (snapshot.numChildren() >= MAX_LOG_COUNT) {
    let childCount = 0;
    const updates = {};
    snapshot.forEach((child) => {
      if (++childCount <= snapshot.numChildren() - MAX_LOG_COUNT) {
        updates[child.key] = null;
      }
    });
    // Update the parent. This effectively removes the extra children.
    return parentRef.update(updates);
  }
  return null;
});
“严格使用”;
const functions=require('firebase-functions');
//聊天历史记录的最大行数。
const MAX_LOG_COUNT=5;
//如果存在超过MAX_LOG_COUNT的值,则删除触发函数的元素所在节点的同级。
//在本例中,我们将保持聊天消息历史记录的最大数量为max_LOG_COUNT。
exports.truncate=functions.database.ref('/chat').onWrite((change)=>{
const parentRef=change.after.ref;
const snapshot=change.after
if(snapshot.numChildren()>=最大日志计数){
让childCount=0;
常量更新={};
snapshot.forEach((子项)=>{
如果(++childCount此云函数(在此处找到:)在实时数据库中实现了您想要的功能:

'use strict';

const functions = require('firebase-functions');

// Max number of lines of the chat history.
const MAX_LOG_COUNT = 5;

// Removes siblings of the node that element that triggered the function if there are more than MAX_LOG_COUNT.
// In this example we'll keep the max number of chat message history to MAX_LOG_COUNT.
exports.truncate = functions.database.ref('/chat').onWrite((change) => {
  const parentRef = change.after.ref;
  const snapshot = change.after

  if (snapshot.numChildren() >= MAX_LOG_COUNT) {
    let childCount = 0;
    const updates = {};
    snapshot.forEach((child) => {
      if (++childCount <= snapshot.numChildren() - MAX_LOG_COUNT) {
        updates[child.key] = null;
      }
    });
    // Update the parent. This effectively removes the extra children.
    return parentRef.update(updates);
  }
  return null;
});
“严格使用”;
const functions=require('firebase-functions');
//聊天历史记录的最大行数。
const MAX_LOG_COUNT=5;
//如果存在超过MAX_LOG_COUNT的值,则删除触发函数的元素所在节点的同级。
//在本例中,我们将保持聊天消息历史记录的最大数量为max_LOG_COUNT。
exports.truncate=functions.database.ref('/chat').onWrite((change)=>{
const parentRef=change.after.ref;
const snapshot=change.after
if(snapshot.numChildren()>=最大日志计数){
让childCount=0;
常量更新={};
snapshot.forEach((子项)=>{

如果(++childCount Javasricpt和react-Native您可以尝试使用触发器功能。它将工作于您的阵列。如果您计划将阵列从前端推送到firestore,那么您可以将逻辑放在那里。我认为这将类似于arrayunion自动执行,即复制将删除,如果不复制将添加,自动化Javasricpt和react-Native您可以尝试使用触发功能。它将使您的阵列工作。如果您计划将阵列从前端推送到firestore,那么您可以将逻辑放在那里。我认为这将像arrayunion自动执行一样,即复制将删除,如果不复制,将添加、自动化