简体   繁体   English

如何将具有值的集合中的所有文档以 map firestore web 的形式推送到另一个集合

[英]How to push all the documents in a collection with values to another collection in form of map firestore web

I have products in my cart which on checkout get converted to orders.我的购物车中有产品,在结账时会转换为订单。 I want all the documents in cart collection to be mapped as an object map / array in orders collection inside a single document.我希望购物车集合中的所有文档都映射为单个文档中订单集合中的 object map / 数组。

function placeOrder(){
    var uid = auth.currentUser.uid
    db.collection('users').doc(uid).collection('carts')
    .get().then((items)=>{
        items.forEach((item)=>{
            var item_key = item.id
           db.collection('users').doc(uid).collection('orders').doc().set({
               items : [item_key]
           })
           db.collection('users').doc(uid).collection('carts').doc(item_key).delete()
        })       
    })
}

Order item IDs => Array field:订单商品 ID => 数组字段:

...
.get()
.then(snapshot => {
    const ids = snapshot.docs.map(doc => doc.id)

    db.collection('users')
        .doc(uid)
        .collection('orders')
        .doc()
        .set({items : ids})
}

Order item data => Sub-Collection:订单商品数据 => 子系列:

...
.get()
.then(snapshot => {
    const batch = db.batch()
    const orderRef = db.collection('users').doc(uid).collection('orders').doc()
    const cartRef = db.collection('users').doc(uid).collection('carts').doc(item_key)

    batch.set(orderRef, { 
        // order fields
    })

    snapshot.forEach(docSnap => {
        batch.set(orderRef.collection('order_items').doc(docSnap.id), {
            // order item fields
        })
    })

    batch.delete(cartRef)
    batch.commit()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Firestore 中“获取集合中的所有文档”? - How to "Get all documents in a collection" in Firestore? 如何删除firestore集合数据库中的所有文档 - How to delete all the documents in a firestore collection database 如何获取 firestore 集合中的所有文档? - How to get all documents in a collection in firestore? 如何对一个集合中多个文档的值求和并将总和推送到另一个集合中的文档 - How to sum the values from multiple documents in one collection and push total sum to document in another collection 如何获取 Firestore 集合中所有文档的子集合的特定文档? - How to get specific Documents of a Subcollection of all Documents in a Collection in Firestore? Firestore 规则:如何检查集合中所有文档的 map 的内容? - Firestore rules: How do I check the contents of a map on all documents in a collection? 如何从 React Native 中的 Firestore 获取集合中的所有文档 - How to get all documents in a collection from Firestore in React Native 如何从 Firestore 的集合中获取所有文档 - How to get all documents from a collection from Firestore 如何获取 Firestore (v9) 子集合中的所有文档 - How to get all documents in Firestore (v9) sub collection 如何检查集合中的所有文档以获取值 Firestore reactjs - How to check all documents in a collection for a value firestore reactjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM