Hướng dẫn create array field mongodb

Docs HomeMongoDB Manual

Nội dung chính

  • How do I create an array of data in MongoDB?
  • How do you do query an array in MongoDB explain with an example?
  • How do I query an array object in MongoDB?
  • Can we insert array in MongoDB?

On this page

  • Match an Array
  • Query an Array for an Element
  • Specify Multiple Conditions for Array Elements
  • 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 queries for all documents where the field tags value is an array with exactly two elements, "red" and "blank", in the specified order:

If, instead, you wish to find an array that contains both the elements "red" and "blank", without regard to order or other elements in the array, use the $all operator:

The following example queries for all documents where tags is an array that contains the string "red" as one of its elements:

For example, the following operation queries for all documents where the array dim_cm contains at least one element whose value is greater than 25.

When specifying compound conditions on array elements, you can specify the query such that either a single array element meets these condition or any combination of array elements meets the conditions.

The following example queries for documents where the dim_cm array contains elements that in some combination satisfy the query conditions; e.g., one element can satisfy the greater than 15 condition and another element can satisfy the less than 20 condition, or a single element can satisfy both:

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

The following example queries for documents where the dim_cm array contains at least one element that is both greater than ($gt) 22 and less than ($lt) 30:

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

Note

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

The following example queries for all documents where the second element in the array dim_cm is greater than 25:

Use the $size operator to query for arrays by number of elements. For example, the following selects documents where the array tags has 3 elements.

For additional query examples, see:

  • Query Documents

  • Query on Embedded/Nested Documents

  • Query an Array of Embedded Documents


You can use the concept of toArray() to create array. Following is the syntax −

db.yourCollectonName.find({}, {yourFieldName:1}).toArray();

Let us create a collection with documents −

> db.createArrayDemo.insertOne({"UserName":"Chris"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cbd6461de8cc557214c0e00")
}
> db.createArrayDemo.insertOne({"UserName":"David"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cbd6467de8cc557214c0e01")
}
> db.createArrayDemo.insertOne({"UserName":"Robert"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cbd646cde8cc557214c0e02")
}
> db.createArrayDemo.insertOne({"UserName":"Sam"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cbd6470de8cc557214c0e03")
}

Display all documents from a collection with the help of find() method −

> db.createArrayDemo.find().pretty();

This will produce the following output −

{ "_id" : ObjectId("5cbd6461de8cc557214c0e00"), "UserName" : "Chris" }
{ "_id" : ObjectId("5cbd6467de8cc557214c0e01"), "UserName" : "David" }
{ "_id" : ObjectId("5cbd646cde8cc557214c0e02"), "UserName" : "Robert" }
{ "_id" : ObjectId("5cbd6470de8cc557214c0e03"), "UserName" : "Sam" }

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.

> db.createArrayDemo.find({},{_id:0}, {UserName:1}).toArray();

This will produce the following output −

[
   {
      "UserName" : "Chris"
   },
   {
      "UserName" : "David"
   },
   {
      "UserName" : "Robert"
   },
   {
      "UserName" : "Sam"
   }
]

Case 2 − Create array with MongoDB with field name_id only

If you want to create an array with field name _id only, use the below query.

> db.createArrayDemo.find({}, {_id:1}).toArray();

This will produce the following output −

[
   {
      "_id" : ObjectId("5cbd6461de8cc557214c0e00")
   },
   {
      "_id" : ObjectId("5cbd6467de8cc557214c0e01")
   },
   {
      "_id" : ObjectId("5cbd646cde8cc557214c0e02")
   },
   {
      "_id" : ObjectId("5cbd6470de8cc557214c0e03")
   }
]

Updated on 30-Jul-2019 22:30:25

  • Related Questions & Answers
  • MongoDB query with $all in array
  • Query array of nested string with MongoDB?
  • MongoDB query to update array with another field?
  • MongoDB query for array concatenation?
  • Query a nested field within an array with MongoDB
  • Filter query on array of embedded document with MongoDB?
  • Query on the last object of an array with MongoDB
  • MongoDB query to insert an array element with a condition?
  • MongoDB query to concatenate values of array with other fields
  • MongoDB query to fetch array values
  • Query array of subdocuments in MongoDB
  • MongoDB query to sort nested array?
  • MongoDB query to aggregate nested array
  • Query MongoDB with length criteria?
  • Query MongoDB collection starting with _?

How do I create an array of data in MongoDB?

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.

How do you do query an array in MongoDB explain with an example?

To query if the array field contains at least one element with the specified value, use the filter { : } where is the element value. To specify conditions on the elements in the array field, use query operators in the query filter document: { : { : , ... } }

How do I query an array object 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.

Can we insert array in MongoDB?

MongoDB Array of Objects using insert() with Example The “insert” command can also be used to insert multiple documents into a collection at one time. The below code example can be used to insert multiple documents at a time. The output shows that those 3 documents were added to the collection.