So I have a mongo database with collections that have fields set to a type of array.
{
"_id" : ObjectId("kdsfisdasg924837529dsfhk"),
"myArr" : [
123456
]
}
I'm looking to query this collection and find all documents, that do not have the elements in myArr
set to a data type of long. I've been digging around, and I haven't found anything referencing a solution. I was trying something like:
db.myCollection.find({myArr: {$not: {$type: "long"}}},{myArr:1, _id: 0})
If i pass .count()
to it, it returns a large number of documents. Almost the entire collection's document count, which I know is incorrect. I'm guessing I'm using $not
incorrectly. Is my syntax wrong? is there a different approach to solving this?
UPDATE
I believe I have solved the problem of finding the documents that have arrays without elements of the type long
using: db.myCollection.find({myArr: {$elemMatch: {$not: {$type: "long"}}}}, {myArr:1, _id: 0})
. How would one go about recasting or changing all those element's types to long
?
For Refernce
Im getting pretty close. I believe I can modify this function I wrote to iterate over the array thats within the documents returned with the above function.
db.myCollection.find({myArr: {$elemMatch: {$not: {$type: 18}}}}).forEach(function(myDoc){
for(var i =0; i < myDoc.myArr.length; i++){
print(typeof myDoc.myArr[i]);
}
});