How to get all objects of array?

I have the following data in a MongoDB.

{
	"experiences": {
		"Project0": [{
				"title": "AA"
			},
			{
				"title": "BB",
				"sub": "B"

			},
			{
				"title": "CC",
				"sub": "C"
			}
		],
		"Project1": [
			{
				"title": "AA",
				"sub": "A"

			}
		]
		
	}
}

I want to get Project0 and Project1 back from a GraphQL query. My current mongoose.Schema only retrieves data for “Project0”.

Book.js

import mongoose from "mongoose";

const pastProjectsSchema =  new mongoose.Schema({
    experiences:{
        Project0:[
                {   
                    title: String,
                    sub: String
                }
            ]
    }
})
export const Experience = mongoose.model( "Experience", pastProjectsSchema , "experiences");

typeDefs.js

import gql from 'graphql-tag';

export const typeDefs = gql`
    type Query {
        experiences: [Experiences]
    }

    type Experiences{
            experiences:Experience
    }

    type Experience {
        Project0: [ProjectDetail]
      }
    
      type ProjectDetail {
        title: String
        sub: String
      }
       
`;

resolvers.js

import {  Experience} from './models/Book.js'

export const resolvers = {
    Query: {
        experiences: async() => await Experience.find({}),
    }
};

Hi @lindylex,

Welcome to the MongoDB Community forums :sparkles:

It appears that the schema code currently only includes the Project 0 field, which will require some modifications to properly align with the data you shared from your collection in MongoDB.

Would you mind sharing how you inserted the data into your MongoDB collection, such as whether you used an insert query from the mongo shell or an API call from your application? This information would be helpful in ensuring that the schema accurately reflects the data in your collection.

Perhaps, we could update the schema to better match the collection data. Here’s an example of what the schema would look like:

const pastProjectsSchema = new mongoose.Schema({
  experiences: {
    Project0: [
      {   
        title: String,
        sub: String
      }
    ],
    Project1: [
      {   
        title: String,
        sub: String
      }
    ]
  }
})

Also after modifying the schema, you will need to update your typeDefs and resolvers as well to include Project1.

Regards,
Kushagra