0

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!

2
  • Hi there! To clarify, do you want the timerSchema.users property to hold an array of arrays? Commented Jun 25, 2020 at 19:29
  • @Tunmee thanks for getting to me! Yes, exactly. Commented Jun 25, 2020 at 19:41

1 Answer 1

2

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>
      }
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! You helped me a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.