简体   繁体   English

在lodash中使用MongoDB ObjectIds

[英]Using MongoDB ObjectIds with lodash

I keep running into trouble when working with ObjectIds and lodash. 使用ObjectIds和lodash时,我总是遇到麻烦。 Say I have two arrays of objects I want to use lodash _.unionBy() with: 说我有两个对象数组,我想使用lodash _.unionBy()

var arr1 = [
    {
        _id: ObjectId('abc123'),
        old: 'Some property from arr1',
    },
    {
        _id: ObjectId('def456'),
        old: 'Some property from arr1',
    },
];

var arr 2 = [
    {
        _id: ObjectId('abc123'),
        new: 'I want to merge this with object in arr1',
    },
    {
        _id: ObjectId('def456'),
        new: 'I want to merge this with object in arr1',
    },
];

var res = _.unionBy(arr1, arr2, '_id');

Result 结果

console.log(res);
/*
[
    {
        _id: ObjectId('abc123'),
        old: 'Some property from arr1',
    },
    {
        _id: ObjectId('def456'),
        old: 'Some property from arr1',
    },
    {
        _id: ObjectId('abc123'),
        new: 'I want to merge this with object in arr1',
    },
    {
        _id: ObjectId('def456'),
        new: 'I want to merge this with object in arr1',
    },
]
*/

Desired result 所需结果

[
    {
        _id: ObjectId('abc123'),
        old: 'Some property from arr1',
        new: 'I want to merge this with object in arr1',
    },
    {
        _id: ObjectId('def456'),
        old: 'Some property from arr1',
        new: 'I want to merge this with object in arr1',
    },
]

Since ObjectIds are objects and they are not pointing to the same reference in many cases (eg when fetching documents from MongoDB and comparing with local seed for testing), I cannot use '_id' as iteratee. 由于ObjectId是对象,并且在许多情况下(例如,从MongoDB提取文档并与本地种子进行比较以进行测试时),它们并没有指向相同的引用,因此我不能使用'_id'作为iteratee。

How do I use lodash with ObjectIDs to achieve desired results? 如何将lodash与ObjectID结合使用以达到期望的结果?

Try this, I removed ObjectId because it does not work in javascript. 试试这个,我删除了ObjectId,因为它在javascript中不起作用。 You can use .toString for string conversion. 您可以使用.toString进行字符串转换。

 var arr1 = [{ _id: 'abc123', old: 'Some property from arr1', }, { _id: 'def456', old: 'Some property from arr1', }, ]; var arr2 = [{ _id: 'abc123', new: 'I want to merge this with object in arr1', }, { _id: 'def456', new: 'I want to merge this with object in arr1', }, ]; const data = arr2.reduce((obj, ele) => { if (!obj[ele._id]) obj[ele._id] = ele.new; return obj; }, {}) arr1 = arr1.map((d) => { if (data[d._id]) { d.new = data[d._id]; } return d; }) console.log(arr1); 

You'll have to use _.unionWith that allows you to use a custom comparator. 您将必须使用_.unionWith ,它允许您使用自定义比较器。 Use the custom comparator to check equality between the two ObjectIds: 使用自定义比较器检查两个ObjectId之间的相等性:

_.unionWith(arr1, arr2, (arrVal, othVal) => arrVal._id.equals(othVal._id));

Hope it helps. 希望能帮助到你。

This is what ended up solving my problem. 这最终解决了我的问题。

var res = arr1.map(a => {
  return _.assign(a, _.find(arr2, { _id: a._id }));
});

Thanks to Tushar's answer 谢图莎尔的回答

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM