您可以将$where运算符与一些脚本一起使用。
首先让我们创建一个包含文档的集合-
> dbsameValueMultipleTimesDemoinsertOne( { "ListOfPrice":[ {"Price": 110}, {"Price":130}, {"Price": 145} ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cefc4e6ef71edecf6a1f6b9") } > dbsameValueMultipleTimesDemoinsertOne( { "ListOfPrice":[ {"Price": 110}, {"Price":178}, {"Price": 110} ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cefc4e7ef71edecf6a1f6ba") }
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> dbsameValueMultipleTimesDemofind()pretty();
输出结果
{ "_id" : ObjectId("5cefc4e6ef71edecf6a1f6b9"), "ListOfPrice" : [ { "Price" : 110 }, { "Price" : 130 }, { "Price" : 145 } ] } { "_id" : ObjectId("5cefc4e7ef71edecf6a1f6ba"), "ListOfPrice" : [ { "Price" : 110 }, { "Price" : 178 }, { "Price" : 110 } ] }
以下是在数组中多次查找相同值的查询-
> dbsameValueMultipleTimesDemofind({ "$where": function() { return thisListOfPricefilter(function(p) { return pPrice == 110; })length > 1; } })
输出结果
{ "_id" : ObjectId("5cefc4e7ef71edecf6a1f6ba"), "ListOfPrice" : [ { "Price" : 110 }, { "Price" : 178 }, { "Price" : 110 } ] }