简体   繁体   English

mongo db,节点express,从mongodb返回的输出中提取元素

[英]mongo db , node express , extract an element from output returned by mongodb

for first block of code , console.log("ff : "+ff) prints exactly like this : 对于第一段代码,console.log(“ ff:” + ff)的输出结果完全一样:

ff : { _id : sd845y3hishofiuwhei , fullname : 'sachin' } ff:{_id:sd845y3hishofiuwhei,全名:“ sachin”}

console.log("ff JSON : "+JSON.stringify(ff)); // this line prints like below : 

ff JSON : [{"_id":"sd845y3hishofiuwhei" , "fullname" : "sachin" }] ff JSON:[{“ _ id”:“ sd845y3hishofiuwhei”,“全名”:“ sachin”}]

how can i get that "sachin" separately in a separate variable ? 我怎样才能在一个单独的变量中分别获得“ sachin”?

var fullname=SampleModel.find({uname:req.session.uname},{fullname : true},function(err,ff){
    if(!err){
        console.log("ff : "+ff);
        console.log("ff JSON  : "+JSON.stringify(ff));
        return ff;
    }else { 
        return null;
    }
});

the below block of code is giving error like "invalid object id " . 下面的代码块给出了诸如“无效的对象ID”之类的错误。 what can be the problem ? 可能是什么问题?

var ret= AnotherModel.find({_id : { $nin : ["1","2"] } }, function(err,details){
    if(!err) {
        res.send(details);
    }
    else {
        console.log("Error : "+err);
        res.send(err);
    }
});

i am implementing textarea like this 我正在像这样实现textarea

<textarea id="myarea" cols="100" rows="20"   ></textarea>

output box is changing its height when i change rows , but that cols , when i change "cols" , its width not changing . 当我更改行时,输出框会更改其高度,但是当我更改“ cols”时,输出框的宽度不会更改。 the output box also has small width , i check in both chrome and mozilla. 输出框的宽度也较小,我同时检查了chrome和mozilla。 Whats the solution ? 有什么解决方案? Thanks 谢谢

I understand that you use mongoose and not only mongodb driver. 我知道您使用的是mongoose,而不仅仅是mongodb驱动程序。

In the first block of code, the ff value isn't a string but an array of objects. 在第一段代码中,ff值不是字符串,而是对象数组。

You can access "sachin" variable just doing something like : var fullname = ff[0].fullname; 您可以执行以下操作来访问“ sachin”变量: var fullname = ff[0].fullname; Of course, it's best to iterate through the array but you get the idea. 当然,最好遍历数组,但是您可以理解。

If you want only one object to be retrieved, you should use "findOne" function instead and then you would be able to access "sachin" like this : var fullname = ff.fullname; 如果只想检索一个对象,则应改用“ findOne”函数,然后便可以像这样访问“ sachin”: var fullname = ff.fullname;

For the second question, you should use Query system. 对于第二个问题,您应该使用查询系统。 Queries are delayed requests that you can chain and tune more finely. 查询是延迟的请求,您可以对其进行更精细的链接和调整。 To delay your query, just ommit the callback function : var query = Model.find({}); 要延迟查询,只需省略回调函数: var query = Model.find({});

Then, add the filter you want, in your case : query.where('_id').nin(['1', '2']); 然后,根据需要添加所需的过滤器: query.where('_id').nin(['1', '2']);

Finally, execute it : 最后,执行它:

query.exec(function (err, docs) {
    // called when the `query.complete` or `query.error` are called
    // internally
});

Informations about finding docs and about queries . 有关查找文档查询的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM