猫鼬find()返回模型的属性

时间:2019-03-26 06:18:54

标签: node.js mongoose

我正在尝试使用猫鼬找到现有用户 我的查询是

UserAccount.find(variables, function (error, success) {
            if (error) {
                response.send({status: false});
            } else {
                console.log(success);
            }
        }); 

如果用户存在,则它将返回以下数组。

[ model {
        '$__':
         InternalCache {
           strictMode: true,
           selected: [Object],
           shardval: undefined,
           saveError: undefined,
           validationError: undefined,
           adhocPaths: undefined,
           removing: undefined,
           inserting: undefined,
           saving: undefined,
           version: undefined,
           getters: {},
           _id: 5c98e64f2106e94022532c9f,
           populate: undefined,
           populated: undefined,
           wasPopulated: false,
           scope: undefined,
           activePaths: [StateMachine],
           pathsToScopes: {},
           session: null,
           ownerDocument: undefined,
           fullPath: undefined,
           emitter: [EventEmitter],
           '$options': [Object] },
        isNew: false,
        errors: undefined,
        _doc:
         { isActive: true,
           _id: 5c98e64f2106e94022532c9f,
           userName: 'buyer@mysite.com',
           password:
            '$2a$05$vpowA76cB3T/4eHGbQPqd.F/iIebX7SXKPZA2k1wcmlSIDks0q852',
           userCategory: 'buyer',
           createdDate: 2019-03-20T14:31:43.250Z,
           updatedDate: 2019-03-20T14:31:43.250Z,
           __v: 0 },
        '$init': true } ]

我不知道是什么引起了这个问题?直到昨天,它只返回用户数据,但这对我来说很奇怪。如何解决这个问题?有人可以帮我解决这个问题吗?谢谢。

4 个答案:

答案 0 :(得分:1)

我也面临着同样的问题,探针是:如果您将最新版本的mongo与较旧的mongoose结合使用,则可以通过安装较新的mongoose版本并运行mongoose find()来解决,这将解决您的探针。

答案 1 :(得分:0)

您最初获得的额外属性是由于以下事实:结果是模型实例的集合,这些模型实例带有普通对象无法使用的其他属性和方法。 您可以将{lean: true}作为选项获得普通对象,而无需所有这些其他属性和方法。

UserAccount.find(variables, {lean: true}, function (error, success) {
            if (error) {
                response.send({status: false});
            } else {
                console.log(success);
            }
        }); 

答案 2 :(得分:0)

您只需在查询末尾使用lean()即可摆脱这种情况

const courses = await Course
    .find({ isPublished: true })
    .or([
        { price: { $gte: 15 } },
        { name: /.*by.*/i }
    ]).lean(); // use lean to remove unnecessary values from output 

答案 3 :(得分:0)

如果你只是在没有使用 find() 方法的情况下登录控制台,只需添加 console.log(success.toObject())

相关问题