fils capo
fils capo

Reputation: 316

How to I find documents in mongo which does not contain a array element

I have 2 kinds of documents in my mongodb

first is like this with a array of interests

{
    "_id" : ObjectId("5e7e4c3ad2b96e109e0d3842"),
    "email" : "[email protected]",
    "interesse" : [ 
        "interest 1", 
        "interest 2", 
        "interest 3"
    ],
    "data" : 20170130,
    "name" : "testename",
    "gender" : "F"
}

and the second is like this with a big string of interests separated by /

{
    "_id" : ObjectId("5e7e4cbbd2b96e109e6b748f"),
    "email" : "[email protected]",
    "interesse" : "interest 3 / interest 4 / interest 5 / interest 6 / interest 7",
    "data" : 20190122,
    "name" : "user2",
    "gender" : "F"
}

I want to select only the data with the big string.. so i can apply a my "stringToArray" function ..

using .find({interesse:{$type:'array'}}) i can get all the type1
but if i use

.find({interesse:{$type:'string'}}) i will get all  registers in my base.

using

.find({interesse:{$type:{$not:'array'}}}) 

i get

Error: error: {
    "ok" : 0,
    "errmsg" : "type must be represented as a number or a string",
    "code" : 14,
    "codeName" : "TypeMismatch"
}

what can i do to solve this?

Upvotes: 1

Views: 923

Answers (1)

juanlews
juanlews

Reputation: 44

try

.find({interesse:{$not:{$type:'array'}}})

Upvotes: 1

Related Questions