Hướng dẫn create array field mongodb
Docs Home → MongoDB Manual Show Nội dung chính
On this page
➤ 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 If, instead, you wish
to find an array that contains both the elements The following example queries for all documents where For example, the following operation queries for all documents where the array 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 Use The following example queries for documents where the 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. NoteWhen 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 Use the For additional query examples, see:
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
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 { 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. |