In MongoDB mapreduce, how can I flatten the values object?

前端 未结 7 2172
挽巷
挽巷 2020-12-04 22:22

I\'m trying to use MongoDB to analyse Apache log files. I\'ve created a receipts collection from the Apache access logs. Here\'s an abridged summary of what my

7条回答
  •  一向
    一向 (楼主)
    2020-12-04 22:27

    Taking the best from previous answers and comments:

    db.items.find().hint({_id: 1}).forEach(function(item) {
        db.items.update({_id: item._id}, item.value);
    });
    

    From http://docs.mongodb.org/manual/core/update/#replace-existing-document-with-new-document
    "If the update argument contains only field and value pairs, the update() method replaces the existing document with the document in the update argument, except for the _id field."

    So you need neither to $unset value, nor to list each field.

    From https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#cursor-snapshot "MongoDB cursors can return the same document more than once in some situations. ... use a unique index on this field or these fields so that the query will return each document no more than once. Query with hint() to explicitly force the query to use that index."

提交回复
热议问题