我不知道如何使用nodejs本地驱动打印mongodb查询结果

时间:2021-04-14 03:03:39

I'm storing a single word in the database using the following:

我在数据库中存储一个单词如下:

collection.update({},{$set:{word:newWord}},{upsert:true},function(){
    collection.find().nextObject(function(err, results) {
        oldWord = results;
        console.log("New Word: " + results);
    });
});

here is an example of my DB

这是我的数据库的一个例子

{ "_id" : ObjectId("4ff92def446ce41df5692385"), "word" : "asdf" }

Every time it gets to the console.log line, it looks like:

每次它到达控制台。对数线,看起来是:

New Word: [object Object]

I'm trying to isolate "asdf" (sans quotes) from the above record. I've tried everything from toArray, nextObject, etc.

我试图将“asdf”(无引号)与上面的记录隔离开来。从toArray, nextObject,等等,我什么都试过了。

What am I missing? I've been trying for hours!

我缺少什么?我已经试了好几个小时了!

3 个解决方案

#1


3  

If you're interested in some field, you should print that, not the whole object (mainly because string representation of objects in Javascript isn't very helpful).

如果您对某个字段感兴趣,您应该打印它,而不是整个对象(主要是因为Javascript中的对象的字符串表示不是很有用)。

 console.log("New Word: " + results.word);

#2


1  

In node, you can just use: console.log("New Word: ", results); (note the comma).

在node中,您可以使用:console。日志(“新词:”,结果);(注意逗号)。

Another option: console.log( JSON.stringify(results) );

另一个选择:控制台。日志(JSON.stringify(结果));

It will print something like: { "_id" : "4ff92def446ce41df5692385", "word" : "asdf" }

它将输出如下内容:{"_id": "4ff92def446ce41df5692385", "word": "asdf"}

I don't really agree that the "string representation of objects in Javascript isn't very helpful." I find it helpful, at least.

我不太同意“Javascript中对象的字符串表示没有多大帮助”。我觉得这至少是有帮助的。

#3


0  

You can either override the toString Method of you result object as described here: http://blog.anselmbradford.com/2009/04/05/object-oriented-javascript-tip-overriding-tostring-for-readable-object-imprints/ or simpy just print the property you want to show by specifying it object.propertyname.

您可以重写result对象的toString方法,如这里所述:http://blog.anselmbradford.com/2009/04/05/object-oriented-javascript-tip-overriding-tostring for read-object -imprints/或者simpy,通过指定object.propertyname来打印您想要显示的属性。

#1


3  

If you're interested in some field, you should print that, not the whole object (mainly because string representation of objects in Javascript isn't very helpful).

如果您对某个字段感兴趣,您应该打印它,而不是整个对象(主要是因为Javascript中的对象的字符串表示不是很有用)。

 console.log("New Word: " + results.word);

#2


1  

In node, you can just use: console.log("New Word: ", results); (note the comma).

在node中,您可以使用:console。日志(“新词:”,结果);(注意逗号)。

Another option: console.log( JSON.stringify(results) );

另一个选择:控制台。日志(JSON.stringify(结果));

It will print something like: { "_id" : "4ff92def446ce41df5692385", "word" : "asdf" }

它将输出如下内容:{"_id": "4ff92def446ce41df5692385", "word": "asdf"}

I don't really agree that the "string representation of objects in Javascript isn't very helpful." I find it helpful, at least.

我不太同意“Javascript中对象的字符串表示没有多大帮助”。我觉得这至少是有帮助的。

#3


0  

You can either override the toString Method of you result object as described here: http://blog.anselmbradford.com/2009/04/05/object-oriented-javascript-tip-overriding-tostring-for-readable-object-imprints/ or simpy just print the property you want to show by specifying it object.propertyname.

您可以重写result对象的toString方法,如这里所述:http://blog.anselmbradford.com/2009/04/05/object-oriented-javascript-tip-overriding-tostring for read-object -imprints/或者simpy,通过指定object.propertyname来打印您想要显示的属性。