How do i create an array of data in mongodb?
You can use the concept of toArray() to create array. Following is the syntax − Show
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
Published May 25, 2020Last updated Jun 02, 2020 1. IntroductionIn a MongoDB database, data is stored in collections and a collection has documents. A document has fields and values, like in a JSON. The field types include scalar types ( The example is an application where users create blog posts and write comments for the posts. The relationship between the posts and comments is One-to-Many; i.e., a post can have many comments. We will consider a collection of blog posts with their comments. That is a post document will also store the related comments. In MongoDB's document model, a 1:N relationship data can be stored within a collection; this is a de-normalized form of data. The related data is stored together and can be accessed (and updated) together. The comments are stored as an array; an array of comment objects. A sample document of the blog posts with comments:
In an application, a blog post is created, comments are added, queried, modified or deleted by users. In the example, we will write code to create a blog post document, and do some CRUD operations with comments for the post. 2. Create and Query a DocumentLet's create a blog post document. We will use a database called as
Returns a result Now, let's query the collection :
The 3. Add an Array ElementLet's add a comment for this post, by a user "DB Learner":
Returns: The Let's query the collection and confirm the new
comment visually, using the
Note the
4. Update an Array ElementLet's update the comment posted by "Database Rebel" with modified
The
5. Delete an Array ElementThe user changed his mind and wanted to delete the comment, and then add a new one.
The A new comment is added to the array after the above delete operation, with the following text: 6. Add a New Field to all Objects in the ArrayLet's add a new field
The all positional operator
7. Update a Specific Array Element Based on a ConditionFirst, let's add another new comment using the
Note the
The 8. ConclusionThis article showed doing basic CRUD operations on an array of objects:
MongoDB also allows indexing the array elements - in this case, fields of the comment objects of the We can do many other operations on the arrays - projections, querying and updating - using various operators as well as the Aggregation Pipeline. Also, perform these operations on nested arrays (arrays within arrays). We will see some of these features in a later article. 8.1. Useful Links
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.
Can you have an array in MongoDB?Unlike relational database models, MongoDB documents can have fields which have values as arrays. The prototypical example in almost all MongoDB documentation is a document having a tags field, whose value is an array of strings, such as ["NoSQL", "Ruby", "MongoDB"] .
How do I push data into an array in MongoDB?In MongoDB, the $push operator is used to appends a specified value to an array. If the mentioned field is absent in the document to update, the $push operator add it as a new field and includes mentioned value as its element. If the updating field is not an array type field the operation failed.
What is an array in MongoDB?The array is significant and useful in MongoDB to define a set of elements in a single set. Different types of array operator available in MongoDB like $, $size, $elemMatch, $pop, $all, $push and $pull. MongoDB array is nothing but a simple list of values in MongoDB.
|