Q
Q
qfrontend2020-08-31 22:16:40
Node.js
qfrontend, 2020-08-31 22:16:40

How to make a lookup in MongoDB return an empty array?

Hello, Please tell me what am I doing wrong?
How to do a lookup in MongoDB?
You need to do a search on the fields (login, password) ....
They are in the nested object (user) .
Why is an empty array returned?

D.B.

{
 _id: ObjectId("5f4d31dc5e7b610a0c7914d2")
 user: {
        login:  "qwerty",
        password:  "123"
    }
}


userSchema.js
const UserSchema = new Schema({
    user: {
        login: String,
        password: String
    },
    jwt: String
});


api.js
router.post('/users/user/:login', (req,res) => {
   User.find({login: req.body.login, password: req.body.password})
   .then((data) => {
       console.log(data);
    })
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-09-01
@hzzzzl

const UserSchema = new Schema({
    user: {
        login: String,
        password: String
    },
    jwt: String
});

here it turns out that each user document will contain a nested object in the spirit , is that what it meant? or is it still
user: { user: {login, password} }
const UserSchema = new Schema({
    login: String,
    password: String,
    jwt: String
});

// если так, то сработает
User.find({login: req.body.login, password: req.body.password})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question