0
db.school.find({ "merchant" : "cc8c0421-e7fc-464d-9e1d-37e168b216c3" })

this is an example document from school collection of that query:

    { 
        "_id" : ObjectId("57fafasf2323232323232f57682cd42"), 
        "status" : "wait", 
"merchant" : "cc8c0421-e7fc-464d-9e1d-37e168b216c3",
        "isValid" : false, 
    "fields" : { "schoolid" : {
            "value" : "2323232", 
            "detail" : {
                "revisedBy" : "teacher", 
                "revisionDate" : ISODate("2015-06-24T09:22:44.288+0000")
            }, 
            "history" : [

            ]
        }}
    }

I want to see which has duplcate schoolid. SO i do this:

db.school.aggregate([
{$match:{  "merchant" : "cc8c0421-e7fc-464d-9e1d-37e168b216c3"
  { $group: {
    _id: { fields.schoolid.value: "$fields.schoolid.value" },  
    count: { $sum: 1 } 
  } }, 
  { $match: { 
    count: { $gte: 2 } 
  } },
  { $sort : { count : -1} },
  { $limit : 10 }
]);

but it gives error.

a lot of errors for a lot of lines

i tried to do like this

_id: { "fields.schoolid.value": "$fields.schoolid.value" }, 

or _id: { 'fields.schoolid.value': "$'fields.schoolid.value'" },

but did not work. ow can i use it?

0

1 Answer 1

1

According to the document you provided, there is no fields field, so the group stage can't work. Your query should be :

db.school.aggregate([
  { $match: {  "merchant" : "cc8c0421-e7fc-464d-9e1d-37e168b216c3"}},
  { $group: {
    _id: { value: "$fields.schoolid.value" },  
    count: { $sum: 1 } 
  } }, 
  { $match: { 
    count: { $gte: 2 } 
  } },
  { $sort : { count : -1} },
  { $limit : 10 }
]);

Also note that fields.schoolid.value is not a valid fieldname, you need to enclode it in "" or to remove the "."

Sign up to request clarification or add additional context in comments.

1 Comment

i added fields. sorry. i enclosed it in with "" but still same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.