Problem with mongodb, findOne() returning null

Right now im trying to find specific data in my mongodb using findOne().

I currently have:

CharData.findOne({
            guildID: bot.guilds.cache.get(message.guild.id).id,
            userID: user.id,
            characters: [{
                CharacterID: CharID
            }]
        }, (err, data) => {
            if(err) console.log(err)
            console.log(data)
        })

Im trying to find the data in the characters array and find the specific characterID. (CharID is args[1]).

I tried doing h-s 4f7aeb48-754d-4fe6-832c-5da616e7ea46 (characterID) and for some reason it just returned null

As I said, it just returned null I don’t know what I did wrong. (I want to get the entire object of the character)

Hey @HK420 I think the issue is your query over the array ‘characters’. The Mongo docs say that your query characters : [{CharacterID: CharID}] will be interpreted literally, the elements in the document including their order must match your query, you’re getting null because your document has > 1 elements and the elements are also not the same shape, i think. It might be easier to achieve what you want to do by filtering the result of the following query in your callback:

{
    guildID: bot.guilds.cache.get(message.guild.id).id,
    userID: user.id,
}

EDIT: you might find docs on querying embedded documents in arrays helpful, seems like the following might work:

{
    guildID: bot.guilds.cache.get(message.guild.id).id,
    userID: user.id,
    "characters.CharacterID": CharID,
}
1 Like

Thanks! Ill see if this works!

I did:

CharData.findOne({
            guildID: bot.guilds.cache.get(message.guild.id).id,
            userID: user.id,
            "characters.CharacterID": CharID,
        }, (err, data) => {
            if(err) console.log(err)
            console.log(data)
        })

But it just console.logs() my entire db, it doesn’t only send the filtered data

Hey @HK420, i suppose its a little difficult to debug this without knowing your schema, but what were you expecting? By db do you mean collection or document? its odd that findOne is returning you entire db, should return just one document of the CharData collection.

image

my schema

I think it’s returning the whole schema that has the characterID specified in it.

Welp, fixed it by doing:

        CharData.findOne({
            guildID: bot.guilds.cache.get(message.guild.id).id,
            userID: user.id,
            "characters.CharacterID": CharID,
        }, (err, data) => {
            if(err) console.log(err)
            if(data === null) return message.reply(`\`\`Please specify a characterID. ex. ${config.prefix}status private/public 0a0aaa00-000a-0aa0-000a-0aa000a0aa00\`\``)
            var findByID = Object.values(data.characters).find(e => e.CharacterID === CharID)
            console.log(findByID)
        })

ah right, nice work, yeah findOne() will return the whole document :slight_smile:

Ok now, if anyone else can help. When I do this, I can’t edit the actual data ;-; is there any way I can do this in a way where I can edit the data aswell?

OMG NVM I GOT IT TO WORK!!!

I just did:

        CharData.find({}, {
            characters: {
                $elemMatch: {
                    CharacterID: CharID
                }
            }
        }, (err, data) => {
            if(err) console.log(err)
            console.log(data[0].characters[0])
        })

And with this I can also make a page system where if they have the same UUID by chance, you can select between which character!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.