Mongoose - How add documents to array that has id of that documents

Matias Lopez

I want transform original array stored with id of departments, pushing object document to same array. But when execute got "Converting circular structure to JSON".

Any idea how get all object document referenced by id and after replace the same array?

I create a Mutations.js for manage my functions transform JSON like remove _id to id and add href key with API url.

But first I want add departments object document to department key in original JSON.

Controller User

const boom = require('boom')
const User = require('../models/Users')
const Departments = require('../models/Departments')
const Mutation = require('../mutations')

    // GET all users
    exports.getUsers = async (req, reply) => {
        try {
            const users = await User.find()
            var json = [];
            users.map(item => {
                var element = Mutation.toJSON( item.toObject({ versionKey: false }) );
                element.departments = Mutation.rebuildArray(Departments, item.departments)
                json.push(element);
            })
            return json
        } catch (err) {
            throw boom.boomify(err)
        }
    }

Mutation.js

//Here convert document to JSON with structure needed
const toJSON = document => {
   document.id = document._id;
   delete document._id;
   return document;
}

//Create a array with document searched by id
const rebuildArray = (model, array) => {
      var newArray = [];
      array.map(item => {
        var element = model.findById(item);
      newArray.push(toJSON(element));
   })
   return newArray;
}

exports.rebuildArray = buildArray;
exports.toJSON = toJSON;
exports.addURL = addURL;

I want recieve this JSON for example:

[ { "id": "f98nu4505fd782" "firstname": "John", "lastname": "Doe", "departments": [ { "id": "d8nn83873434", "name": "Department 1 Name" }, { "id": "9d8n892030e9", "name": "Department 2 Name" } ] } ]

With this original JSON:

[ { "id": "f98nu4505fd782" "firstname": "John", "lastname": "Doe", "departments": ["d8nn83873434","9d8n892030e9"] } ]

Thanks!

Cristopher Herrera

You should try to use .populate('departments') of mongoose query.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to query findOne() in mongoose such that we get subset of array of documents which satisfy particular condition?

分類Dev

Display array of documents as objects with id as key

分類Dev

Get all documents of a type in mongoose but only with 1 specific item of each documents array

分類Dev

mongodb/mongoose findMany - find all documents with IDs listed in array

分類Dev

Querying subdocuments array with filter and return original documents with mongoose

分類Dev

How to delete multiple id documents in elasticsearch index

分類Dev

How to create documents and sub-documents at same time in Node Mongo DB mongoose Express App

分類Dev

How to sort documents based on length of an Array field

分類Dev

How to insert multiple documents into sub document at once in mongoose

分類Dev

Elasticsearch: Matching documents with an array in it

分類Dev

Mongoose sum fields from populated documents

分類Dev

Is there an elegant way to render two sets of mongoose documents?

分類Dev

How to split an array of JSON documents into fixed size chunks?

分類Dev

MongoDB How to update the first item that matches a condition in an array of documents?

分類Dev

How to filter documents in a collection when inside an array in Arangodb?

分類Dev

how to find all documents in MongoDB between HH:mm format time using mongoose?

分類Dev

How to make a query using Mongoose that gets N results, but combines any documents it finds that meet certain criteria?

分類Dev

How to find null documents in mongodb?

分類Dev

Alfresco : How to filter documents in workflow

分類Dev

MongoDB aggregate - average on specific values in array of documents

分類Dev

Find Documents Where a Field Compares with Another in an Array

分類Dev

Mongoose, how to iteratively add an array of subDocs from an array

分類Dev

How to make a RecyclerView only with documents that have in their array a number larger than X

分類Dev

How to get all documents from a firestore collection and return them in an array list?

分類Dev

How to convert ISO Date to 'yyyy-mm-dd hh:mm:ss' in mongodb from embedded array of documents?

分類Dev

MongoDB, finding all documents where property id equals to record id

分類Dev

WordVBAを使用した「Documents.Open」と「Documents.Add」

分類Dev

How to stop insertion of Duplicate documents in a mongodb collection

分類Dev

How to replace multiple documents in one DB call

Related 関連記事

  1. 1

    How to query findOne() in mongoose such that we get subset of array of documents which satisfy particular condition?

  2. 2

    Display array of documents as objects with id as key

  3. 3

    Get all documents of a type in mongoose but only with 1 specific item of each documents array

  4. 4

    mongodb/mongoose findMany - find all documents with IDs listed in array

  5. 5

    Querying subdocuments array with filter and return original documents with mongoose

  6. 6

    How to delete multiple id documents in elasticsearch index

  7. 7

    How to create documents and sub-documents at same time in Node Mongo DB mongoose Express App

  8. 8

    How to sort documents based on length of an Array field

  9. 9

    How to insert multiple documents into sub document at once in mongoose

  10. 10

    Elasticsearch: Matching documents with an array in it

  11. 11

    Mongoose sum fields from populated documents

  12. 12

    Is there an elegant way to render two sets of mongoose documents?

  13. 13

    How to split an array of JSON documents into fixed size chunks?

  14. 14

    MongoDB How to update the first item that matches a condition in an array of documents?

  15. 15

    How to filter documents in a collection when inside an array in Arangodb?

  16. 16

    how to find all documents in MongoDB between HH:mm format time using mongoose?

  17. 17

    How to make a query using Mongoose that gets N results, but combines any documents it finds that meet certain criteria?

  18. 18

    How to find null documents in mongodb?

  19. 19

    Alfresco : How to filter documents in workflow

  20. 20

    MongoDB aggregate - average on specific values in array of documents

  21. 21

    Find Documents Where a Field Compares with Another in an Array

  22. 22

    Mongoose, how to iteratively add an array of subDocs from an array

  23. 23

    How to make a RecyclerView only with documents that have in their array a number larger than X

  24. 24

    How to get all documents from a firestore collection and return them in an array list?

  25. 25

    How to convert ISO Date to 'yyyy-mm-dd hh:mm:ss' in mongodb from embedded array of documents?

  26. 26

    MongoDB, finding all documents where property id equals to record id

  27. 27

    WordVBAを使用した「Documents.Open」と「Documents.Add」

  28. 28

    How to stop insertion of Duplicate documents in a mongodb collection

  29. 29

    How to replace multiple documents in one DB call

ホットタグ

アーカイブ