avoronkin / mpopuate

Minimalistic population for node-mongodb-native

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mpopuate

Minimalistic population for node-mongodb-native

Build Status Coverage Status

npm install mpopulate
const mc = require('mongodb').MongoClient
const mpopulate = require('mpopulate')

const db = await mc.connect('mongodb://localhost:27017/mpopulate')

const posts = await db.collection('posts').find().toArray()

await mpopulate([
    {
        localField: 'author comments.user',
        from: 'users',
        foreignField: '_id',
    },
    {
        localField: 'comments',
        from: 'comments',
        foreignField: '_id',
    }
],{
    mongodb: {
        db
    }
})(posts, {
    author: {name: 1}
})

assert.deepEqual(posts[0], {
    _id: id(1),
    title: 'title1',
    author: {
        _id: id(1),
        name: 'user1'
    },
    comments: [ id(1), id(2), id(3), id(4) ]
})

About

Minimalistic population for node-mongodb-native

License:MIT License


Languages

Language:JavaScript 100.0%