1

I have a Module called Organisation with an array calls users in it that contains UserSchema objects. Now i need a query to get all users from all organisation documents in one array.

As you can see I am a beginner in mongodb and normaly use sql But without joins I don´t know what to do.

OrganisationModule:

const OrganisationSchema = new Schema({
  name: { type: String, required: true },
  users: [UserSchema],
  version: String,
});

module.exports.Organisation = mongoose.model('Organisation', OrganisationSchema);

UserSchema:

module.exports.UserSchema = new Schema({
  name: String,
  roles: [String]
})

My first try:

routes.get('/', (req, res, next) => {
Organisation.find().populate('users').exec((err, users) => {
  if (err) res.json(err.message)
  else { res.json(users) }
});

The result:

[
  {
    "users": [
      {
        "roles": [ "coordinator" ],
        "_id": "5aafcf80dd248f7ef86e0512",
        "name": "Peter"
        "__v": 0
      }
    ],
    "_id": "5aafcf80dd248f7ef86e05cf",
    "name": "DEFAULT",
    "__v": 1
  },
  {
    "users": [
      {
        "roles": [ "admin", "coordinator" ],
        "_id": "5aafcf80dd248f7ef86e0500",
        "name": "Max"
        "__v": 0
      }
    ],
    "_id": "5aafcf80dd248f7ef86e05ce",
    "name": "Organisation_01",
    "__v": 1
  }
]

What I need:

[
  {
    "roles": [ "coordinator" ],
    "_id": "5aafcf80dd248f7ef86e0512",
    "name": "Peter"
    "__v": 0
  },
  {
    "roles": [ "admin", "coordinator" ],
    "_id": "5aafcf80dd248f7ef86e0500",
    "name": "Max"
    "__v": 0
  }
]
A. Stretz
  • 21
  • 4

2 Answers2

1

This

Organization.find(
  {},
  {_id: 0, users: 1}
)

Will return

[
  {
    users: {
      roles: ['coordinator'],
      _id: '5aafcf80dd248f7ef86e0512',
      name: 'Peter',
      ....
    },
  },
  {
    users: {
      roles: ['admin', 'coordinator'],
      _id: '5aafcf80dd248f7ef86e0500',
      name: 'Max',
      ....
    },
  },
];

This is not precisely what you want, but it is what I have found that most closely matches your need.

You can find more information here :

Else, there is an other approach

// Meaby you will need to add await
const users = Organization.find({}).map((item: any) => item.users);
avimimoun
  • 799
  • 9
  • 23
-1

In mongodb you can use $lookup to perform that operation.

Please study $lookup here: https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

In mongoose you can use populate()

For example:

Organization.find().populate('users')

Study Mongoose's Populate here: http://mongoosejs.com/docs/populate.html

Farhan Tahir
  • 2,096
  • 1
  • 14
  • 27
  • Thanks for your answer. My problem is that populate returns me an array of organisations. It does not join all users into one array – A. Stretz Apr 07 '18 at 08:37
  • @A.Stretz I guess you want to get all users of specific organization ? Because populate is returning correct data, users array belonging to the appropriate organization object. – Farhan Tahir Apr 07 '18 at 09:01
  • 2
    No, I whant **all** users from **all** organisations in 1 array like in the example I added. For exaple: If I have 2 organisations each with 3 users I´ll get an array of 6 users. – A. Stretz Apr 07 '18 at 10:11