Can you have an array of objects in mongodb?

I'm new to MongoDb, I have the same problem that Stennie describes. I tried to query this document in Compass:

    {
    "_id": 1,
    "name": {
        "first": "John",
        "last": "Backus"
    },
    "birth": {
        "$date": "1924-12-02T23:00:00.000Z"
    },
    "death": {
        "$date": "2007-03-16T23:00:00.000Z"
    },
    "contribs": ["Fortran", "ALGOL", "Backus-Naur Form", "FP"],
    "awards": [{
        "award": "National Medal",
        "year": 1975,
        "by": "NSF"
    }, {
        "award": "Turing Award",
        "year": 1977,
        "by": "ACM"
    }]
}

When to run this query:{awards: {$elemMatch: {award:'National Medal', year:1975}}} for this single document, I get the same result:

{
    "_id": 1,
    "name": {
        "first": "John",
        "last": "Backus"
    },
    "birth": {
        "$date": "1924-12-02T23:00:00.000Z"
    },
    "death": {
        "$date": "2007-03-16T23:00:00.000Z"
    },
    "contribs": ["Fortran", "ALGOL", "Backus-Naur Form", "FP"],
    "awards": [{
        "award": "National Medal",
        "year": 1975,
        "by": "NSF"
    }, {
        "award": "Turing Award",
        "year": 1977,
        "by": "ACM"
    }]
}

It is possible to obtain the single element searched for in this way:

{
    "_id": 1,
    "name": {
        "first": "John",
        "last": "Backus"
    },
    "birth": {
        "$date": "1924-12-02T23:00:00.000Z"
    },
    "death": {
        "$date": "2007-03-16T23:00:00.000Z"
    },
    "contribs": ["Fortran", "ALGOL", "Backus-Naur Form", "FP"],
    "awards": [{
        "award": "National Medal",
        "year": 1975,
        "by": "NSF"
    }, {}]

Thank you

Docs HomeMongoDB Manual

On this page

  • Query for a Document Nested in an Array
  • Specify a Query Condition on a Field in an Array of Documents
  • Specify Multiple Conditions for Array of Documents
  • Additional Query Tutorials


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.


The following example selects all documents where an element in the instock array matches the specified document:

Equality matches on the whole embedded/nested document require an exact match of the specified document, including the field order. For example, the following query does not match any documents in the inventory collection:

If you do not know the index position of the document nested in the array, concatenate the name of the array field, with a dot (.) and the name of the field in the nested document.

The following example selects all documents where the instock array has at least one embedded document that contains the field qty whose value is less than or equal to 20:

Using dot notation, you can specify query conditions for field in a document at a particular index or position of the array. The array uses zero-based indexing.

Note

When querying using dot notation, the field and index must be inside quotation marks.

The following example selects all documents where the instock array has as its first element a document that contains the field qty whose value is less than or equal to 20:

When specifying conditions on more than one field nested in an array of documents, you can specify the query such that either a single document meets these condition or any combination of documents (including a single document) in the array meets the conditions.

Use $elemMatch operator to specify multiple criteria on an array of embedded documents such that at least one embedded document satisfies all the specified criteria.

The following example queries for documents where the instock array has at least one embedded document that contains both the field qty equal to 5 and the field warehouse equal to A:

The following example queries for documents where the instock array has at least one embedded document that contains the field qty that is greater than 10 and less than or equal to 20:

If the compound query conditions on an array field do not use the $elemMatch operator, the query selects those documents whose array contains any combination of elements that satisfies the conditions.

For example, the following query matches documents where any document nested in the instock array has the qty field greater than 10 and any document (but not necessarily the same embedded document) in the array has the qty field less than or equal to 20:

The following example queries for documents where the instock array has at least one embedded document that contains the field qty equal to 5 and at least one embedded document (but not necessarily the same embedded document) that contains the field warehouse equal to A:

For additional query examples, see:

  • Query an Array

  • Query Documents

  • Query on Embedded/Nested Documents

Can we store array of objects in MongoDB?

One of the benefits of MongoDB's rich schema model is the ability to store arrays as document field values. Storing arrays as field values allows you to model one-to-many or many-to-many relationships in a single document, instead of across separate collections as you might in a relational database.

How do I query an array of objects in MongoDB?

To search the array of object in MongoDB, you can use $elemMatch operator. This operator allows us to search for more than one component from an array object.

How do I add an array of documents in MongoDB?

insertMany() can insert multiple documents into a collection. Pass an array of documents to the method. The following example inserts three new documents into the inventory collection. If the documents do not specify an _id field, MongoDB adds the _id field with an ObjectId value to each document.

How do I create an array in MongoDB query?

Build a To-Do List App with Node, Express, React and MongoDB. Case 1 − Create array with MongoDB. If you want to create an array of field UserName and do not want the field _id, use the below query. If you want to create an array with field name _id only, use the below query.