我在查询一个集合并获取Map结构中的数据,就像这样-
eventData : [{
eventUID: rQigoeThdP0thE9oChld,
eventLocation: Bldg-4,
eventName: Test Prod 0217,
eventStatus: Complete,
eventContacts: [{
contactName: Anna Leochal,
contactTitle: Station Dir,
contactUID: L2z9Jhr3i49YVBLzA4ta
}, {
contactName: Eleonore Hendricks,
contactTitle: Station Manager,
contactUID: ArklRd6Ggivcvi9OMHvj
}, {
contactName: Tom Warner,
contactTitle: Station Asst Mgr,
contactUID: 0bfGw5kaYgseKTxshbMW
}
],
eventDateModified: Timestamp(seconds = 1676668006, nanoseconds = 962000000),
eventDateCreated: Timestamp(seconds = 1676667859, nanoseconds = 944000000)
}
]
我想做的是从由传递的参数myContactUID标识的eventContacts部分中删除数据。例如,传入myContactUID =“0 bfGw 5 kaYgseKTxshbMW”将从eventConcats中删除Tom Warner的数据。
我就是在这里被困住的。我需要从eventContacts中删除与Tom的contactUID(contactName,contactTitle,contactUID)关联的整个对象
如果我将eventContacts转换为List,即。final List myEventContacts = document.get(FieldPath(const [“eventContacts”]))!作为列表;我得到-
myEventContacts: [{
contactName: Anna Leochal,
contactTitle: Station Dir,
contactUID: L2z9Jhr3i49YVBLzA4ta
}, {
contactName: Eleonore Hendricks,
contactTitle: Station Manager,
contactUID: ArklRd6Ggivcvi9OMHvj
}, {
contactName: Tom Warner,
contactTitle: Station Asst Mgr,
contactUID: 0bfGw5kaYgseKTxshbMW
}
]
我试过各种方法-
var eventUpdate = {};
eventUpdate[ eventConcats.contactUID : {[myContactUID]: firebase.firestore.FieldValue.delete();}];
db.collection('events').doc('$myEventDocID').set(eventUpdate);
因此─
WriteBatch myEventDocBatch = FirebaseFirestore.instance.batch();
db
.collection('events')
.where('eventUID', isEqualTo: myEventUID)
.get()
.then((querySnapshot) {
var qsEventsLen = querySnapshot.docs.length;
var qsEventsSize = querySnapshot.size;
print('qsEventsLen: $qsEventsLen ');
print('qsEventsSize: $qsEventsSize ');
querySnapshot.docs.forEach((document) {
// do something with myBatch
// myEventDocBatch.delete(document.reference);
var myEventDocID = document.id; // i.e. 6eWyqjfEdG4ZJlepoHSf
print('myEventDocID: $myEventDocID');
var myEventUID = document.get(FieldPath(const ["eventUID"]))!
as String; // i.e. rQigoeThdP0thE9oChld
print('myEventUID: $myEventUID');
final eventData = document.data();
print("eventData: ${document.data()}");
final List myEventContacts =
document.get(FieldPath(const ["eventContacts"]))! as List;
print("myEventContacts: ${myEventContacts}");
var eventUpdate = {};
eventUpdate[ eventConcats.contactUID : {[myContactUID]: firebase.firestore.FieldValue.delete();}];
db.collection('events').doc('$myEventDocID').set(eventUpdate);
});
return myEventDocBatch.commit();
});
print('End: Get Event Doc');
但毫无结果。我所做的调查似乎对这件事都没有帮助。我只是更困惑了。
任何想法将是最受欢迎的
以前的研究:Cloud Firestore: Update fields in nested objects with dynamic key
Firebase firestore nested query
Query map object data in dart/flutter based on key value
How to delete object from array in firestore
1条答案
按热度按时间ghhkc1vu1#
我想明白了必须将myEventContacts List转换为Map并迭代值,然后删除索引处找到的项。我不认为这是最有效的路线,所以,如果你有一个想法,如何简化这一点,我很乐意看到它!否则,我希望这对别人有帮助。