MongoDB data are as follows
> db.foo.find()
{ "_id" : ObjectId("5ae84b29fca5249951cadc26"), "id" : 1, "datas" : [ { "content_type" : 0 } ] }
{ "_id" : ObjectId("5ae84b29fca5249951cadc27"), "id" : 2, "datas" : [ { "content_type" : 0 }, { "content_type" : 0 } ] }
{ "_id" : ObjectId("5ae84b29fca5249951cadc28"), "id" : 3, "datas" : [ { "content_type" : 2 }, { "content_type" : 2 } ] }
{ "_id" : ObjectId("5ae84b29fca5249951cadc29"), "id" : 4, "datas" : [ { "content_type" : 0 }, { "content_type" : 2 } ] }
{ "_id" : ObjectId("5ae84c01fca5249951cadc2a"), "id" : 5, "datas" : [ { "content_type" : 2 } ] }
requirements
confirm that the content_type value of each object in the datas array is the same, that is, all are 0 or 2
confirm statement
> db.foo.aggregate(
... {$unwind:"$datas"},
... {$group:{"_id":{id:"$id",content_type:"$datas.content_type"}}},
... {$group:{"_id":"$_id.id",count:{$sum:1}}},
... {$match:{count:{$gt:1}}}
... )
{ "_id" : 4, "count" : 2 }
indicates that there is a situation where the value of content_type is not unique in the datas array
I wonder if there is any other way to confirm whether the values of a certain field of all the objects in the array are the same, that is, all the objects in the array belong to the same class
.