MongoDB query to Retrieve all the rows which contain a particular element inside an array

MongoDB query to Retrieve all the rows which contain a particular element inside an array

mongodb return only matching array elements
mongodb-query nested array of objects
mongodb query array of objects
mongodb query subdocument array
mongodb find all elements in array
mongodb return all matching array elements
mongodb query nested object
mongodb java query array contains

I'm new to using MongoDb and I'm stuck in this query , I want to retrieve all the rows that have small size available

{
  "_id":ObjectId("562e7c594c12942f08fe4192"),
  "sizes":["small","medium","large"]
},
{
  "_id":ObjectId("562e7c594c22942f08fe4193"),
  "sizes":["small"]
},
{
  "_id":ObjectId("562e7c594c12942f08fe4199"),
  "sizes":["medium","large"]
},

I want all the rows that have small size available i.e

{
  "_id":ObjectId("562e7c594c12942f08fe4192"),
  "sizes":["small","medium","large"]
},
{
  "_id":ObjectId("562e7c594c22942f08fe4193"),
  "sizes":["small"]
},

I don't know what commands you have already tried, but ,maybe try using this command:

myCursor = db.inventory.find( { 'sizes': "small" } )

Since this will only show you the first 20 documents, you can place a loop:

while (myCursor.hasNext()) {
    print(tojson(myCursor.next()));
}

ref: https://docs.mongodb.com/guides/server/read_queries/

MongoDB Arrays Tutorial: How to Query String Values (With , The $all operator selects the documents where the value of a field is an array that contains all the specified elements. To specify an $all expression, use the� To query if the array field contains at least one element with the specified value, use the filter { <field>: <value> } where <value> is the element value. To query if the array field contains at least one element with the specified value, use the filter eq ( <field>, <value>) where value is the element value.


You need to use $elemMatch

xxx.find(
   { sizes: { $elemMatch: { $eq:"small" } } }
)

https://docs.mongodb.com/manual/reference/operator/query/elemMatch/

MongoDB Query operator: $all, MongoDB Manual - how to query an array of documents, how to query an array of all documents where an element in the instock array matches the specified document: When querying using dot notation, the field and index must be inside the instock array has as its first element a document that contains the field qty� Note. In most cases, MongoDB does not treat arrays as sets. This operator provides a notable exception to this approach.


try this one

db.sales.aggregate([
 {
    $project: {
         items: {
            $filter: {
                input: "$items",
                as: "item",
                cond: { $gte: [ "$$item.sizes", small] }
            }
          }
      }
   }
}
])

or

db.users.find({sizes': "small"});

Query an Array of Embedded Documents — MongoDB Manual, This query selects all documents in the inventory collection where the tags field holds either a string that starts with be or st or an array with at least one element� With the use of the $ array projection operator, you can specify the projection to return the first element that match the query condition on the array field; e.g. "arrayField.$": 1. (Not available for views.) <field>: <array projection>


$in — MongoDB Manual, The $ operator projects the first matching array element from each document in a in the query, or if you need to project based on multiple fields in the array's The following find() operation queries for all documents where the value of first matching element of the students array where the school field has a value of 102 :. Use the Array Index to Query for a Field in the Embedded Document¶ 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.


$elemMatch (projection) — MongoDB Manual, To return all documents in a collection, omit this parameter or pass an empty to return the first element that match the query condition on the array field; e.g. A projection cannot contain both include and exclude specifications, with the transaction incurs a greater performance cost over single document writes, and the� The query will return only documents that contain the Gaming value in the interests array. It does not matter where the Gaming value falls within the array or how many values are included in the array. All that matters is that the array contains the specified element. Using $elemMatch to query arrays


db.collection.find(), Each person has a cars property, which is an array of car objects, each find() query, we get the entire document with all the array items returned: a single object from the array - namely, the object that has a matching value� Retrieve all documents in the MongoDB query on the employee collection by using the MongoDB connection. documents is a structure array that contains a structure for each document returned by the query. documents = find (conn,collection, 'Query',mongoquery); Close the MongoDB connection.