问题标签 [node-mongodb-native]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1052 浏览

node.js - 包装抛出的错误,这样它们就不会使节点网络服务器崩溃(node-mongodb-native)

简化代码......但基本场景是我正在使用 Mongo 进行 findOne 查询以查找用户,但如果用户不存在,则会引发导致整个网络服务器崩溃的错误。有人能指出我正确的方向来正确包装这些错误,这样他们就不会把一切都搞砸了吗?

路线:

DBError 声明:

这是处理错误的块:

现在,当我在这里运行单元测试时是堆栈跟踪,然后结束服务器进程(不理想!):

0 投票
1 回答
311 浏览

node.js - 根据日期Upsert到Mongo?

我想做的是将value字段应用于具有 id 的对象,只有当日期比当前保存的日期更新时。如果没有保存具有该 ID 的文档,则只需保存此文档。

我正在使用 node-mongodb-native。有没有办法在不先让文档检查日期并再次保存的情况下做到这一点?

谢谢

0 投票
1 回答
4267 浏览

mongodb - 如何格式化 mongodb find 查询的结果?

我有一个以这种格式存储数据的集合:

我希望能够仅以这种格式公开 varA 数据(没有嵌套深度......):

不幸的是,我的查询(我使用投影时):

返回这样的东西:

如何改进查找查询以返回预期格式?

非常感谢那些会帮助我的人;-)

PS这里我使用mongo shell来检索数据,但我需要让这个查询与node.js和node-mongodb-native一起工作。

0 投票
1 回答
662 浏览

node.js - Nodejs,MongoDB(node-mongodb-native)。FindAndModify 找到 doc,但不更新它

我正在尝试使用findAndModify操作来查找和更新文档。

我在我的回调函数中得到了 Doc(所以它通过查询找到),但是 MongoDB 文档中没有任何变化。相同的命令在 mongo 控制台中运行良好。它改变了我需要的一切。我究竟做错了什么?

所以整个命令看起来像:

0 投票
0 回答
349 浏览

mongodb - How can I get a native driver database context with Mongoose?

I'm using Mongoose, and I'd like to use the native driver for some things. One of the things I'd like to do is make a query for a GridFS file. You normally do that as such:

db.fs.files.findOne({});

However, mongoose.connection.db doesn't have fs defined. So I need a native driver db object, not the Mongoose one. Is there a way that I can get such an object from Mongoose?

0 投票
1 回答
2218 浏览

javascript - MongoDb 日期格式

我正在使用 NodeJs(本机驱动程序)批量插入 MongoDB。我在数据中有日期字段。反正有没有将日期字段存储为Date而不是String

我有日期dd/mm/yyyy格式。在当前情况下,我通过迭代将日期转换为mm/dd/yyyy格式的批量数据来获得结果,然后创建新的日期并保存。

由于随着数据量的增加迭代花费了太多时间;还有其他方法吗?

0 投票
1 回答
1451 浏览

node.js - 如何从 node-mongo 本机驱动程序获取 db 实例?

考虑一下,我在主app.js文件本身中打开了 MongoDB 连接,以下代码属于它的回调:

这一切都是为了在整个应用程序中只有一个数据库实例。

现在,如果我们在另一个external.js文件中并且需要一个db已经连接的相同对象。如果我们使用mongoskinmongoose ,这可以很容易地完成

有人可以帮我找到如何使用本机驱动程序来完成吗?

0 投票
1 回答
536 浏览

javascript - 与 node-mongodb-native 的多个连接


我正在研究使用 node-mongodb-native 模块在 mongoDb 数据库中插入文档的功能。一切正常,除非我连续调用插入多个文档。我使用for循环来测试我的函数如何同时对多个文档插入做出反应。

当我运行这段代码时,我得到了错误为了解决这个问题,db object already connecting, open cannot be called multiple times 我决定在每次调用函数时创建一个新的实例:ServerDb

但是现在我connection closed被 db_open 函数抛出了,我真的不明白为什么我的连接在我创建的那一刻db和我的代码调用的那一刻之间关闭db_open
你知道发生了什么吗?

谢谢 :)

(对不起,如果我的英语不是很好)

编辑 我发现这个网站解释说问题是由太长的 tcp_keepalive 时间引起的。这个解决方案的问题是我的工作站(Cloud 9)。我无权访问文件 /proc/sys/net/ipv4/tcp_keepalive_time

0 投票
1 回答
531 浏览

node.js - DB Connection in mongo-db native client

I have an express/nodeJs app which will use mongo-db native client for persistance in Mongo-db. Now my problem is that most of the example I have seen have one collection and therefore do the connection in that js file, like this tutorial(which is mentioned in mongo-db native client docs). There the connection code is like this:

There are other methods also which I kept out to keep it small(check in the above url for full tutorial).

My problem is that I have few more collections and therefore I am worried as to how to make a single connection to the database and use it for all the collections. I would also like if you can specify how to make connections to replica-sets also for reads and the main database for writes.

Or should I make calls to connections in each of my collection.js files like the above mentioned tutorial has done in one. Please help me.

0 投票
2 回答
124 浏览

mongodb - mongodb update array any matching index

I have a data structure like this:

I want to update the documents such that any students who is 7 years old is changed to school 101. Is there any way to do this in 1 operation?