Javascript 安萨科。通过提供带有merge:true的SetOptions,可以将这些调用配置为执行粒度合并,而不是覆盖目标文档的全部内容
你可以用这个Javascript 安萨科。通过提供带有merge:true的SetOptions,可以将这些调用配置为执行粒度合并,而不是覆盖目标文档的全部内容,javascript,arrays,object,firebase,google-cloud-firestore,Javascript,Arrays,Object,Firebase,Google Cloud Firestore,你可以用这个 const yourNewArray = [{who: "first@test.com", when:timestamp} {who: "another@test.com", when:timestamp}] collectionRef.doc(docId).set( { proprietary: "jhon", sharedWith: firebase.firestore.FieldVal
const yourNewArray = [{who: "first@test.com", when:timestamp}
{who: "another@test.com", when:timestamp}]
collectionRef.doc(docId).set(
{
proprietary: "jhon",
sharedWith: firebase.firestore.FieldValue.arrayUnion(...yourNewArray),
},
{ merge: true },
);
希望这有帮助:)这太令人沮丧了……但谢谢你让我知道我没有发疯。这是一个很大的缺点,谷歌必须尽快解决。@Dougalante的回答表明这已经解决了。使用
arrayUnion
方法。是否有任何方法从数组更新特定索引?如何将此数组更新功能与“react native firebase”一起使用?(我在react native firebase的官方文档中找不到这一点)@ArturCarvalho否,此视频中解释了为什么需要在客户端执行此操作,请使用“从'firebase/app'导入*作为firebase”;然后使用“firebase.firestore.FieldValue.arrayUnion(新元素)”如果有一种方法可以使用特定id更新数组中的项,那就太好了。就像arrayUnion,但有一个merge:true。此时需要2个操作来删除数组项,然后使用新数据再次添加它。在if语句中,您应该将其更改为此,因为您缺少documentReference
adduserRef
,如图所示:transaction.set(userRef,{bookings:[booking]})
@nifCody,这确实会在现有数组“regions”中添加一个新的字符串元素“greater_virginia”。我已经成功地测试了它,而且绝对没有添加“object”。这与“推新记录”的问题是一致的。请解释你的答案。事实上,请阅读嘿,你找到答案了吗?我仍然找不到答案。虽然这可能会回答问题,但如果可能的话,您应该在您的答案中包含此代码块如何回答问题的简短解释。这有助于提供上下文,并使你的答案对未来的读者更有用。@hoppeduppen但你是对的。我绝对接受批评,因为我没有为我的答案添加解释。我已经编辑了我的答案。希望它现在能帮上大忙。
// With SET
firebase.firestore()
.collection('proprietary')
.doc(docID)
.set(
{ sharedWith: [{ who: "third@test.com", when: new Date() }] },
{ merge: true }
)
// With UPDATE
firebase.firestore()
.collection('proprietary')
.doc(docID)
.update({ sharedWith: [{ who: "third@test.com", when: new Date() }] })
firebase.firestore()
.collection('proprietary')
.doc(docID)
.set(
{ sharedWith: [{ who: "third@test.com", when: new Date() }] },
{ merge: true }
)
firebase.firestore()
.collection('proprietary')
.doc(docID)
.collection('sharedWith')
.add({ who: "third@test.com", when: new Date() })
// say you have have the following object and
// database structure as you mentioned in your post
data = { who: "third@test.com", when: new Date() };
...othercode
addSharedWith(data) {
const postDocRef = this.afs.collection('posts').doc('docID');
postDocRef.subscribe( post => {
// Grab the existing sharedWith Array
// If post.sharedWith doesn`t exsit initiated with empty array
const foo = { 'sharedWith' : post.sharedWith || []};
// Grab the existing sharedWith Array
foo['sharedWith'].push(data);
// pass updated to fireStore
postsDocRef.update(foo);
// using .set() will overwrite everything
// .update will only update existing values,
// so we initiated sharedWith with empty array
});
}
const booking = { some: "data" };
const userRef = this.db.collection("users").doc(userId);
this.db.runTransaction(transaction => {
// This code may get re-run multiple times if there are conflicts.
return transaction.get(userRef).then(doc => {
if (!doc.data().bookings) {
transaction.set({
bookings: [booking]
});
} else {
const bookings = doc.data().bookings;
bookings.push(booking);
transaction.update(userRef, { bookings: bookings });
}
});
}).then(function () {
console.log("Transaction successfully committed!");
}).catch(function (error) {
console.log("Transaction failed: ", error);
});
proprietary: "John Doe"
sharedWith:{
whoEmail1: {when: timestamp},
whoEmail2: {when: timestamp}
}
var whoEmail = 'first@test.com';
var sharedObject = {};
sharedObject['sharedWith.' + whoEmail + '.when'] = new Date();
sharedObject['merge'] = true;
firebase.firestore()
.collection('proprietary')
.doc(docID)
.update(sharedObject);
proprietary: "John Doe"(a document)
things(collection of John's things documents)
thingsSharedWithOthers(collection of John's things being shared with others):
[thingId]:
{who: "first@test.com", when:timestamp}
{who: "another@test.com", when:timestamp}
then set thingsSharedWithOthers
firebase.firestore()
.collection('thingsSharedWithOthers')
.set(
{ [thingId]:{ who: "third@test.com", when: new Date() } },
{ merge: true }
)
List<String> list = java.util.Arrays.asList("A", "B");
Object[] fieldsToUpdate = list.toArray();
DocumentReference docRef = getCollection().document("docId");
docRef.update(fieldName, FieldValue.arrayUnion(fieldsToUpdate));
addToCart(docId: string, prodId: string): Promise<void> {
return this.baseAngularFirestore.collection('carts').doc(docId).update({
products:
firestore.FieldValue.arrayUnion({
productId: prodId,
qty: 1
}),
});
}
var documentRef = db.collection("Your collection name").doc("Your doc name")
documentRef.update({
yourArrayName: firebase.firestore.FieldValue.arrayUnion("The Value you want to enter")});
const yourNewArray = [{who: "first@test.com", when:timestamp}
{who: "another@test.com", when:timestamp}]
collectionRef.doc(docId).set(
{
proprietary: "jhon",
sharedWith: firebase.firestore.FieldValue.arrayUnion(...yourNewArray),
},
{ merge: true },
);