Paul
Paul

Reputation: 354

Indexing items in array - MongoDB

I'm quite new to mongodb, hence I would like to know how the mongoose schema should look like when I need to add indexed items in an array.

Here is how I want the output to look like:

_id: some_id
users: Object
  0: Array
      0: some_user_id
      1: some_user_id
      2: some_user_id

This is the schema I tried to create, but I think I'm wrong here:

const mongoose = require('mongoose')

const timerSchema = mongoose.Schema({
  users: [[]]
})

module.exports = timerSchema

Thank you in advance!

Upvotes: 0

Views: 78

Answers (1)

Dimple Patel
Dimple Patel

Reputation: 718

If you want timerSchema.users property to hold an array of arrays then your schema definition is correct.you can also specify type of array, refer This example to create array of arrays using mongoose Schema which contain output as well.

or if you want users as object then,

const timerSchema = mongoose.Schema({
  users: {
         <Field1>:[[]],
         <Field2>:<Type>
      }
})

Upvotes: 2

Related Questions