How to find documents using a field of an embedded document that is inside an array? - mongodb

I have the Data/Schema for my student records:
{
"_id" : ObjectId("579ed0ba7d509178a97fae8f"),
"fullName" : "ABC",
"enroll" : "AB1234",
"profile" : {
"isCompleted" : true,
"verification" : [
{
"pro" : true,
"verifiedBy" : "ProAct",
"verifiedOn" : ISODate("2016-09-12T07:36:53.680Z")
},
],
"isChecked" : false,
"fullName" : "ABC",
"gender" : "Male",
"emergencyContactPerson" : {
"name" : "Father",
"mobile" : "9412345678",
"email" : "example#gmail.com",
},
},
"contact" : {
"emailID" : {
"isVerified" : true,
"isChecked" : false,
"verificationTokenExpiresIn" : ISODate("2016-08-01T05:35:00.218Z"),
"verifiedOn" : ISODate("2016-08-01T04:35:10.992Z")
},
"mobileID" : {
"isVerified" : true,
"isChecked" : false,
"verificationTokenExpiresIn" : ISODate("2016-08-01T04:42:45.206Z"),
"verifiedOn" : ISODate("2016-08-01T04:33:36.692Z")
}
},
"services" : [
{
"applied" : true,
"appliedOn" : ISODate("2016-09-17T03:01:49.829Z"),
"status" : "Created",
"mac" : "70-77-88-00-AA-BB",
"createdBy" : "Example"
},
]
}
How can I build query, to get student info with the help of mac address 70-77-88-00-AA-BB. i only know mac address value?
i am new here please help me out

Query
services.mac is an array of all the mac addresses members of services
in querying(not in aggregation $eq) an array is equal with a value if it contains that value, so the above will work
the bellow query returns all the students, that have that mac adrress on the services array
if you want to make it fast you can also create an index on "services.mac"
*read mongodb documentation its very good and with examples
Test code here
db.collection.find({
"services.mac": "70-77-88-00-AA-BB"
})

Related

MongoDB: group all documents that dont have their Object id inside documents in another collection

so we have a sales site with a glitch that some times a buy order wont go to the shopping cart. I have the task of creating a query to find all the documents inside the orders collection, with a open status, that dont have their ObjectID inside any document, with orderState = edit, in the cart collection
Heres an exemple of a document in the orders collection:
{
"_id" : ObjectId("5c5dac90c7f16568a5df67e5"),
"shipment" : {
"invoice" : "",
"carrying" : "",
"billingDate" : null,
"shipmentDate" : null,
"internalID" : "",
"externalID" : ""
},
"version" : NumberInt(0),
"integrated" : false,
"splited" : false,
"totalValue" : NumberInt(0),
"totalQty" : NumberInt(0),
"mainSupplierOrder" : true,
"id" : "",
"shop" : ObjectId("5c5d88dcb60645517cf64990"),
"supplier" : ObjectId("5c50bca84359b945410a2c2f"),
"status" : "open",
"config" : {
"discount" : NumberInt(0),
"paymentMethod" : "",
"operation" : "100.01",
"obs" : "",
"conference" : false
},
"products" : [
],
"history" : [
],
"createdDate" : ISODate("2019-02-08T16:21:36.644+0000"),
"lastModifiedDate" : ISODate("2019-02-08T16:21:36.644+0000"),
"lastModifiedBy" : "user.name",
"__v" : NumberInt(0),
"isOpen" : true
}
and an exemple of a document in the carts collection:
{
"_id" : ObjectId("5c5de236b47d8ddf670a21e8"),
"calc" : ObjectId("62b40160e0f39d6cf5d9e3e9"),
"shop" : ObjectId("5c5d88dcb60645517cf64969"),
"createdBy" : "useremail#email.com.br",
"lastModifiedBy" : "user.name",
"orderState" : "edit",
"orders" : [
ObjectId("5c5de26ab47d8ddf670a23b0"),
ObjectId("62b1f84c2b9a7103926ab44c"),
ObjectId("62b1f84c2b9a718e376ab449"),
ObjectId("62b1f84c2b9a7130486ab444")
],
"lastModifiedDate" : ISODate("2022-06-27T14:13:23.293+0000"),
"createdDate" : ISODate("2019-02-08T20:10:30.460+0000"),
"__v" : NumberInt(1)
}
I must find all open orders that dont have their ObjectID inside the orders field in the carts documents with the field orderState = edit
Thanks in advance for any help

MongoDB unable to lookup docs based on variable parent document property

I want to find products and for each product attach deals to it. A deal is a product from same collection, yet based on some common properties.
So as per my requirement pipeline should return documents, for each document find other products those aren't same as current, but have equal detail.duration. But even though I've many docs with same duration, deals are always []. Could you please figure out the issue with my pipeline?
Following is the aggregation pipeline I'm running:
I've added filter _id $in just for clarity based on shown documents below. This isn't a part of real pipeline $match query.
db.products
.aggregate([
{
$match: {
_id: {
$in: [
ObjectId("6210fa8746bee3fcbd0ad062"),
ObjectId("6210fa7c46bee3fcbd0acc21"),
],
},
"detail.duration": { $gt: 0 },
},
},
{
$lookup: {
from: "products",
let: { id: "$_id", duration: "$detail.duration" },
as: "deals",
pipeline: [
{
$match: {
_id: { $ne: "$id" },
"detail.duration": "$duration",
},
},
{ $project: { detail: 1 } },
{ $limit: 1 },
],
},
},
{ $limit: 2 },
{ $project: { deals: 1 } },
])
.pretty();
This was the result:
{ "_id" : ObjectId("6210fa7c46bee3fcbd0acc21"), "deals" : [ ] }
{ "_id" : ObjectId("6210fa8746bee3fcbd0ad062"), "deals" : [ ] }
Following are two example documents in the collection:
{
"_id" : ObjectId("6210fa8746bee3fcbd0ad062"),
"book" : "https://wegotrip.com/en/paris-d3/muse-d-orsay-and-musee-de-l-orangerie-combined-tour-ticket-p1117/?SUB_ID=336264",
"address" : "Rue de Lille, 62bis",
"countryName" : "France",
"cityName" : "Paris",
"location" : {
"lang" : 48.859886,
"lat" : 2.3254821,
"country" : ObjectId("6210fa7746bee3fcbd0aca20"),
"city" : ObjectId("6210fa7746bee3fcbd0aca1c"),
"location" : "Rue de Lille, 62bis",
"_id" : ObjectId("6210fa8746bee3fcbd0ad063")
},
"includes" : [
{
"value" : "Skip-the-line ticket to Orsay Museum",
"included" : true
},
{
"value" : "Skip-the-line ticket to the Musée de l'Orangerie",
"included" : true
},
{
"value" : "Detailed description of the Nymphéas from Claude Monet",
"included" : true
},
{
"value" : "Interesting stories of many great artists and their lives",
"included" : true
},
{
"value" : "An easy walkthrough of the Musée d'Orsay and the Musée de l'Orangerie and their great collection",
"included" : true
},
{
"value" : "Headphones — you should bring your own",
"included" : false
}
],
"price" : {
"priceConcession" : null,
"priceChild" : null,
"price" : 57,
"currency" : ObjectId("6210fa7746bee3fcbd0aca2f"),
"_id" : ObjectId("6210fa8746bee3fcbd0ad064")
},
"detail" : {
"isPass" : false,
"features" : [
{
"key" : "audio_guide",
"value" : "Audio Guide"
}
],
"highlights" : [
"Admire the masterpieces by Monet, Renoir, Degas, Cézanne, and many more",
"Discover one of the finest collections of Impressionist art in the world",
"Visit the Nymphéas by Monet, one of the greatest pieces of Impressionism",
"Explore the Guillaume and Walter collection and find out what makes it unique"
],
"details" : [ ],
"images" : [
{
"id" : 7270,
"description" : "",
"cover" : false,
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/1117/dsc04800/01d0770dcc0cac4c6de0f6eae70742f6.jpg",
"full" : "https://app.wegotrip.com/media/store/1117/dsc04800.jpg"
},
{
"id" : 7269,
"description" : "",
"cover" : false,
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/1117/nympheasannees30salle1parisiennephotorogerviolet/e1270aef1c01391290df71d1f83c8abc.jpg",
"full" : "https://app.wegotrip.com/media/store/1117/nympheasannees30salle1parisiennephotorogerviolet.jpg"
},
{
"id" : 7268,
"description" : "",
"cover" : false,
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/1117/ob1f7c80dsc02414-large/7712cb29e133ee3acb4b2bffbc2ac654.jpg",
"full" : "https://app.wegotrip.com/media/store/1117/ob1f7c80dsc02414-large.jpg"
},
{
"id" : 7267,
"description" : "",
"cover" : false,
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/1117/tuileriesgardensb16dsc00678talrg/47430ab8a257e3ccd2337d7a0d750c57.jpg",
"full" : "https://app.wegotrip.com/media/store/1117/tuileriesgardensb16dsc00678talrg.jpg"
},
{
"id" : 7266,
"description" : "",
"cover" : false,
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/1117/009/54223ef27aac5cd94fe5c20893abf2de.jpg",
"full" : "https://app.wegotrip.com/media/store/1117/009.jpg"
},
{
"id" : 7264,
"description" : "",
"cover" : false,
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/1117/monet-morning-with-weeping-willow/09bf842cc9a9d7eade8d0739f704699f.jpg",
"full" : "https://app.wegotrip.com/media/store/1117/monet-morning-with-weeping-willow.jpg"
}
],
"duration" : 2,
"_id" : ObjectId("6210fa8746bee3fcbd0ad065")
},
"availability" : null,
"subcategory" : [
{
"id" : 6,
"title" : "Sightseeing Tickets & Passes",
"slug" : "sightseeing-tickets-passes"
}
],
"category" : [
{
"id" : 6,
"title" : "Sightseeing Tickets & Passes",
"slug" : "sightseeing-tickets-passes"
}
],
"type" : "Audio Guide",
"description" : "Visit the famous Musee d'Orsay and Musée de l'Orangerie in Paris with this combined self-guided tour! \r\n\r\nNavigate through the maze of exhibition rooms with mobile app and see a collection of works by the Impressionists and Expressionists – Seurat, Cezanne, Gaugin, Monet, Renoir, Manet, Van Gogh, Degas; sculptors like Rodin, Pompon and others. Check out a mini-version of the Statue of Liberty! \r\n\r\nExplore the Nymphéas paintings by Claude Monet, that is called \"the Sistine chapel of Impressionism\". Admire the great works of Picasso, Soutine, Rousseau, Matisse and many others part of the Paul Guillaume and Jean Walter collection. Learn about the style and private life of the artists.\r\n\r\nThe audio-guide will provide you with all the information on the cultural significance of these paintings. Walking through rooms you will understand how revolutionary for those times Manet’s, Cezanne’s and Degas’ creation really was casting doubts on conservative, academic conceptions of 'true art' and offering new techniques and ideas.",
"thumbnail" : "https://app.wegotrip.com/media/CACHE/images/store/1117/013/c0b8cce52cb61ab1f30872e6e93385b4.jpg",
"name" : "Musée d'Orsay/Musée de l'Orangerie Combined Admission Ticket & Audio Tour",
"attractionDescription" : "",
"attractionName" : "Musée d'Orsay & Musée de l'Orangerie",
"attraction" : ObjectId("6210fa8746bee3fcbd0ad056"),
"provider" : {
"rating" : {
"count" : 0,
"average" : null,
"_id" : ObjectId("6210fa8746bee3fcbd0ad067")
},
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/1117/013/c0b8cce52cb61ab1f30872e6e93385b4.jpg",
"slug" : "muse-d-orsay-and-musee-de-l-orangerie-combined-tour-ticket",
"id" : "1117",
"key" : "1",
"_id" : ObjectId("6210fa8746bee3fcbd0ad066")
},
"__v" : 0
}
{
"_id" : ObjectId("6210fa7c46bee3fcbd0acc21"),
"book" : "https://wegotrip.com/en/barcelona-d1/the-dali-museum-in-figueres-p3/?SUB_ID=336264",
"address" : "Pujada del Castell, 43",
"countryName" : "Spain",
"cityName" : "Barcelona",
"location" : {
"lang" : 42.26829425831263,
"lat" : 2.95884132385254,
"country" : ObjectId("6210fa7746bee3fcbd0aca3e"),
"city" : ObjectId("6210fa7746bee3fcbd0aca3a"),
"location" : "Pujada del Castell, 43",
"_id" : ObjectId("6210fa7c46bee3fcbd0acc22")
},
"includes" : [
{
"value" : "Recommendations of places to visit to understand the life of Dali better",
"included" : true
},
{
"value" : "Skip-the-line ticket to Dali Theatre-Museum",
"included" : true
},
{
"value" : "Headphones — you should bring your own",
"included" : false
}
],
"price" : {
"priceConcession" : null,
"priceChild" : null,
"price" : 33,
"currency" : ObjectId("6210fa7746bee3fcbd0aca2f"),
"_id" : ObjectId("6210fa7c46bee3fcbd0acc23")
},
"detail" : {
"isPass" : false,
"features" : [
{
"key" : "audio_guide",
"value" : "Audio Guide"
}
],
"highlights" : [
"Discover Dali's surrealism starting with the building of the museum — it's definitely one of a kind",
"Inside the museum you'll find the most famous and controversial works of the artist",
"Our tour will provide you with insights and exiting facts about Dali's works"
],
"details" : [ ],
"images" : [
{
"id" : 6916,
"description" : "",
"cover" : false,
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/3/figueres-oleguer2/032b55c27bb2cd119bdc7fe6c4b86491.jpeg",
"full" : "https://app.wegotrip.com/media/store/3/figueres-oleguer2.jpeg"
},
{
"id" : 6915,
"description" : "",
"cover" : false,
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/3/sky-monument-statue-golden-museum-yellow-1156442-pxherecom/28c645449a9f45ec1e8ede7b7ffbe30f.jpg",
"full" : "https://app.wegotrip.com/media/store/3/sky-monument-statue-golden-museum-yellow-1156442-pxherecom.jpg"
},
{
"id" : 6914,
"description" : "",
"cover" : false,
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/3/architecture-window-museum-landmark-surrealism-catalonia-800928-pxherecom/43691ba6aecc2ee084c300c150e32a03.jpg",
"full" : "https://app.wegotrip.com/media/store/3/architecture-window-museum-landmark-surrealism-catalonia-800928-pxherecom.jpg"
},
{
"id" : 831,
"description" : "",
"cover" : false,
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/3/figueres-oleguers3k6yoz/b9c3093c79cf50e621e022706af59ad6.jpg",
"full" : "https://app.wegotrip.com/media/store/3/figueres-oleguers3k6yoz.jpg"
},
{
"id" : 832,
"description" : "",
"cover" : false,
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/3/shutterstock82210018/2a2450d4f75edf4549d36f2286b6f19b.jpg",
"full" : "https://app.wegotrip.com/media/store/3/shutterstock82210018.jpg"
},
{
"id" : 833,
"description" : "",
"cover" : false,
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/3/dali-museum-8983261920/aa0d93e475c7b7388bee88ff14f8d795.jpg",
"full" : "https://app.wegotrip.com/media/store/3/dali-museum-8983261920.jpg"
},
{
"id" : 834,
"description" : "",
"cover" : false,
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/3/shutterstock196896461/74fc427d0a27f0aa199ed24f4c51bcc5.jpg",
"full" : "https://app.wegotrip.com/media/store/3/shutterstock196896461.jpg"
}
],
"duration" : 2,
"_id" : ObjectId("6210fa7c46bee3fcbd0acc24")
},
"availability" : null,
"subcategory" : [
{
"id" : 3,
"title" : "Theme Tours",
"slug" : "theme-tours"
},
{
"id" : 1,
"title" : "Culture & History",
"slug" : "culture-and-history"
},
{
"id" : 6,
"title" : "Sightseeing Tickets & Passes",
"slug" : "sightseeing-tickets-passes"
}
],
"category" : [
{
"id" : 3,
"title" : "Theme Tours",
"slug" : "theme-tours"
},
{
"id" : 1,
"title" : "Culture & History",
"slug" : "culture-and-history"
},
{
"id" : 6,
"title" : "Sightseeing Tickets & Passes",
"slug" : "sightseeing-tickets-passes"
}
],
"type" : "Audio Guide",
"description" : "The Dalí Theatre and Museum is a museum of the artist Salvador Dalí in his home town of Figueres, in Catalonia, Spain. Dalí is buried there in a crypt below the stage. \r\n\r\nImmerse yourself in an exciting journey through the world of the genius of surrealism. Reveal the meaning of his ambiguous creations and learn the history of the artist's life. Enjoy the unique world of Dali in this excursion.",
"thumbnail" : "https://app.wegotrip.com/media/CACHE/images/store/001_Ispaniya_Figeras_Teatr-01/783c3a10c34eb40c29f14f704cd9c8d1.jpeg",
"name" : "The Dali Theatre-Museum: Skip-the-Line & Audio Tour",
"attractionDescription" : "",
"attractionName" : "Dali Theatre and Museum",
"attraction" : ObjectId("6210fa7c46bee3fcbd0acc15"),
"provider" : {
"rating" : {
"count" : 0,
"average" : null,
"_id" : ObjectId("6210fa7c46bee3fcbd0acc26")
},
"preview" : "https://app.wegotrip.com/media/CACHE/images/store/001_Ispaniya_Figeras_Teatr-01/783c3a10c34eb40c29f14f704cd9c8d1.jpeg",
"slug" : "the-dali-museum-in-figueres",
"id" : "3",
"key" : "1",
"_id" : ObjectId("6210fa7c46bee3fcbd0acc25")
},
"__v" : 0
}
Both of the above have detail.duration set to 2 and as per query, these 2 should have each other considered as a deal and found in result docs, but query returns deals: [], an empty array. I'm unable to figure out the problem.
From $match (Restrictions)
The $match query syntax is identical to the read operation query syntax; i.e. $match does not accept raw aggregation expressions. To include aggregation expression in $match, use a $expr query expression.
And you need to use $$ to get the variable value.
let
To reference variables in pipeline stages, use the "$$" syntax.
Change the $match stage in the pipeline as:
{
$match: {
_id: {
$ne: "$$id"
},
$expr: {
$eq: [
"$detail.duration",
"$$duration"
]
}
}
}
Sample Mongo Playground

mongodb find $elemMatch with Multiple Fields

everyone. I need your help. For a script I have to search for users by their ldap id, but I have the problem that I get no output when searching.
a user has the following output.
db.users.find({"name" : "John Sample"} ).toArray()
[
{
"_id" : "testid",
"createdAt" : ISODate("2017-05-11T13:49:35.125Z"),
"services" : {
"password" : {
"bcrypt" : ""
},
"ldap" : {
"id" : "12345678",
"idAttribute" : "uid"
},
"resume" : {
"loginTokens" : [ ]
}
},
"username" : "john",
"emails" : [
{
"address" : "john.sample#test.com",
"verified" : true
}
],
"type" : "user",
"status" : "offline",
"active" : true,
"name" : "John Sample",
"_updatedAt" : ISODate("2018-07-05T18:44:22.061Z"),
"roles" : [
"user"
],
"ldap" : true,
"lastLogin" : ISODate("2018-07-05T10:33:00.712Z"),
"statusConnection" : "offline",
"utcOffset" : 2,
"statusDefault" : "offline"
}
]
I used this command but without success.
db.users.find({"services" : {"$elemMatch": {"ldap" : {"$elemMatch": {"id" : "12345678"}}}}}} ).toArray();
[ ]
$elemMatch is used to find elements that match criteria in an array. But services is an document (sub document), not an array. Just use dot notation to query :
db.users.find({"services.ldap.id":"12345678"})

How is it possible in Meteor to publish only selected keys of a MongoDB document?

Hello my question is if i have a nth document of monogDb like
{
"_id" : "PfZ6pn2qrXyHk53sz",
"type" : "manager",
"dob" : ISODate("1980-02-02T18:30:00.000Z"),
"registration" : 1231231231,
"country_code" : "India",
"username" : "asad",
"global_is_active" : true,
"gender" : "male",
"user_photo" : "photo",
"user_creation_ip" : "demo ip",
"is_deleted" : false,
"privileges" : {
"partner" : {},
"consumer" : {}
},
"following" : {
"users" : [],
"partners" : []
},
"followers" : [],
"blogger_info" : {},
"user_verified" : false
}
and when i publish document publish with limited keys like
{
"_id" : "PfZ6pn2qrXyHk53sz",
"type" : "manager",
"user_photo" : "photo",
"privileges" : {
"partner" : {},
"consumer" : {}
},
"following" : {
"users" : [],
"partners" : []
},
"followers" : []
}
when client subscribe the collection get only these keys..
Thank's in advance
Since a publication of a MongoDB is just the result of a query, you can adjust the query accordingly. Instead of
Meteor.publish("rooms", function () {
return Rooms.find({});
});
which would publish all documents from the Rooms collection, you can change the query to something like this:
Meteor.publish("rooms", function () {
return Rooms.find({}, {fields: {secretInfo: 0}});
});
This instruction tells MongoDB / Meteor to publish all fields except the secretInfo field of the collection.
Also see the Meteor documentation for the publish method.

Passing More than one where condition in MONGO DB find query

I am having mongo DB collection namely student with the following document structure,
name:details:date:values
so, for an single name we will have one details list,
That details list will have multiple date lists
And each date list will have multiple values list
{
"_id" : ObjectId("51472e9fd29a736d83c27ca3"),
"name" : "Arun",
"details" : [
{
"date" : "2015-01-17",
"isNew" : false,
"isOld" : true,
"values" : [
{
"money" : "330.0",
"new" : false,
"old" : true,
},
{
"money" : "340.0",
"new" : false,
"old" : true,
}
]
},
{
"date" : "2015-01-17",
"isNew" : false,
"isOld" : false,
"values" : [
{
"money" : "330.0",
"new" : false,
"old" : false,
},
{
"money" : "340.0",
"new" : false,
"old" : false,
}
]
},
{
"date" : "2015-01-17",
"isNew" : true,
"isOld" : false,
"values" : [
{
"money" : "330.0",
"new" : true,
"old" : false,
},
{
"money" : "340.0",
"new" : true,
"old" : false,
}
]
},
{
"date" : "2013-10-19",
"isNew" : true,
"isOld" : false,
"values" : [
{
"money" : "330.0",
"new" : true,
"old" : false,
},
{
"money" : "340.0",
"new" : true,
"old" : false,
}
]
}
]
}
What is need is, i want to SELECT "all the date lists" where "name" : "Arun" and "date" : "2015-01-17",I tried this way and it is not working as expected.I am getting all the dates instead 2015-01-17 in return.
I think only one where condition is working here and that is "name" : "Arun" , Query is not considering "details.date" : "2015-01-17" in where condition.
db.student.find({ "details.date" : "2015-01-17","name" : "Arun" },{"details.date":1}).pretty()
{
"_id" : ObjectId("51472e9fd29a736d83c27ca3"),
"details" : [
{
"date" : "2015-01-17"
},
{
"date" : "2015-01-17"
},
{
"date" : "2015-01-17"
},
{
"date" : "2013-10-19"
}
]
}
I am currently using mongo 1.6.5
Can some one help me to solve this.?
First, you need to upgrade to a current version of MongoDB. 1.6 is now three versions behind the current "major" version.
Second, you need to fix your schema. You say "for an single name we will have one details list" - if you continue adding things into this list/array it will continue growing indefinitely and that's a bad schema design. In addition, it's more correct to group in an array values that you want to fetch together with the document - and in this case you specifically do NOT want to fetch all the values with the document, you only want to fetch an element for a particular date, plus the isOld/isNew fields suggest to me that some of these entries will correspond to out-dated values, and others will be current, and it's a bad idea to lump them together into the same document.
So my recommendation is to change your structure to be multiple documents for each student:
{
"name" : "Arun",
"date" : "2015-01-17",
"isNew" : false,
"isOld" : true,
"values" : [
{
"money" : "330.0",
"new" : false,
"old" : true,
},
{
"money" : "340.0",
"new" : false,
"old" : true,
}
]
},
{
"name" : "Arun",
"date" : "2015-01-17",
"isNew" : false,
"isOld" : true,
"values" : [
{
"money" : "330.0",
"new" : false,
"old" : false,
},
{
"money" : "340.0",
"new" : false,
"old" : false,
}
]
},
{
"name" : "Arun",
"date" : "2015-01-17",
"isNew" : false,
"isOld" : true,
"values" : [
{
"money" : "330.0",
"new" : true,
"old" : false,
},
{
"money" : "340.0",
"new" : true,
"old" : false,
}
]
},
{ "name" : "Arun",
"date" : "2013-10-19",
"isNew" : true,
"isOld" : false,
"values" : [
{
"money" : "330.0",
"new" : true,
"old" : false,
},
{
"money" : "340.0",
"new" : true,
"old" : false,
}
]
}
]
}
Now it'll be much more straight forward to query on various attributes, including being able to query in values as well as outer fields.
Your query is in fact matching both fields. The output is a byproduct of how MongoDB handles arrays. Matching against
"details.date" : "2015-01-17"
will return any documents in the collection where any of the entries from the details field have the right date. It will not just return the individual entries from the details array.
To do this, you may want to look at the $elemMatch operator for projection, to limit the entries in the array that are returned.
Here is your query,
db.sandy.aggregate(
{$unwind : "$details"},
{$match : {"details.date" : "2015-01-17","name" : "Arun"}}
)
And I guess mongo 1.6.5 does not support aggregation. refer to Doc once.