如何从猫鼬中的数组中获取数据? [英] How to get data from array in mongoose?

查看:37
本文介绍了如何从猫鼬中的数组中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 mongoose node.js 和 mongoDB 的新手,我有一个像

这样的数据库架构

项目:{项目名称:字符串",项目经理:字符串",任务:[{任务名称:字符串",timetakeninhrs:"字符串"}]};

所以我想要的是只获取具有特定任务名称的任务的详细信息.我正在编写 sql 脚本,以便您知道我想要什么:

Select taskname,timetakeninhrs from project where taskName ='DB create';

解决方案

$elemMatch 投影运算符会派上用场:

项目.where('task.taskName', 'DB create')//或 where('task.taskName').equals('DB create')..select({_id: 0, task: {$elemMatch: {'taskName': 'DB create'}}).exec(函数(错误,文档){var tasks = docs.map(function(doc){ return doc.task[0]; });控制台日志(任务[0].任务名称);//'数据库创建'console.log(tasks[0].timetakeninhrs);//'3'});

在上面,where() 方法充当 Mongoose 模型的静态辅助方法,它使用链式语法构建查询,而不是指定 JSON 对象.所以

//而不是这样写:Project.find({ 'task.taskName': 'DB create' }, callback);//你可以改写:Project.where('task.taskName', 'DB create');//或者Project.where('task.taskName').equals('DB create');

然后链接 select() 使用 $elemMatch.在 exec() 方法(异步执行查询),您需要传入一个回调,该回调遵循模式 callback(error, results).结果取决于操作:对于findOne(),它是一个可能为空的单个文档,find() 文档列表,count() 文档数,update() 文档数受影响的,等等.在这种情况下,这将返回格式如下的文档数组:

<预><代码>[/* 0 */{任务" : [{"taskName" : "数据库创建","timetakeninhrs" : "3"}]},/* 1 */{任务" : [{"taskName" : "数据库创建","timetakeninhrs" : "9"}]}/* 等等 */]

在您的回调中,您可以进行一些数据操作以获取仅具有您指定的那些属性的对象,因此使用本机 JavaScript map() 函数来创建一个新的数组具有这些字段的对象

I am new to mongoose node.js and mongoDB, I have a db Schema like

Project:{
    projectName:"String",
    projectManager:"String",
    task:[{
           taskName:"String",
           timetakeninhrs:"String"
    }]
};

So what I want is to get only the details of task with particular task name. I am writing sql script so that you can know what I want :

Select taskname,timetakeninhrs from project where taskName ='DB create';

解决方案

The $elemMatch projection operator would come in handy for this:

Project
    .where('task.taskName', 'DB create') // or where('task.taskName').equals('DB create').
    .select({_id: 0, task: {$elemMatch: {'taskName': 'DB create'}})
    .exec(function(err, docs){
        var tasks = docs.map(function(doc){ return doc.task[0]; });
        console.log(tasks[0].taskName); // 'DB create'
        console.log(tasks[0].timetakeninhrs); // '3'
    });

In the above, the where() method acts as a static helper method of the Mongoose model that builds up a query using chaining syntax, rather than specifying a JSON object. So

// instead of writing:
Project.find({ 'task.taskName': 'DB create' }, callback);

// you can instead write:
Project.where('task.taskName', 'DB create');

// or
Project.where('task.taskName').equals('DB create');

and then chain the select() method to project the 'task' array field using $elemMatch. In the exec() method (which executes the query asynchronously), you need to pass in a callback which follows the pattern callback(error, results). What results is depends on the operation: For findOne() it is a potentially-null single document, find() a list of documents, count() the number of documents, update() the number of documents affected, etc. In this case this returns an array of documents in the format:

[
    /* 0 */
    {
        "task" : [ 
            {
                "taskName" : "DB create",
                "timetakeninhrs" : "3"
            }
        ]
    },
    /* 1 */
    {
        "task" : [ 
            {
                "taskName" : "DB create",
                "timetakeninhrs" : "9"
            }
        ]
    }
    /* etc */
]

In your callback you can do a bit of data manipulation to get an object that only has those properties you specified, hence the use of the native JavaScript map() function to create a new array of objects with those fields

这篇关于如何从猫鼬中的数组中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆