如何在猫鼬中进行原始mongodb操作? [英] How to do raw mongodb operations in mongoose?
问题描述
我之所以这样问是因为,当我编写单元测试时,我想删除测试数据库并插入一些初始化数据,还要在测试中检查mongodb中的数据.所以我需要对mongodb进行原始操作.
I'm asking this because when I write unit tests, I want to drop the test database and insert some initialize data, and also check the data in mongodb in testing. So I need raw operations to mongodb.
如何用猫鼬做到这一点?我现在能做的就是创建连接,而在mongoose的官方网站上找不到任何文档.
How to do this in mongoose? What I can do now is just create the connection, and not find any document in mongoose's official site.
var mongoose = require('mongoose');
mongoose.connect('mongo://localhost/shuzu_test');
// get the connection
var conn = mongoose.connection;
但是如何:
- 删除数据库
- 创建收藏集
- 将一些数据写入集合
- 查询集合
- 删除收藏集
推荐答案
请参阅文档中有关驱动程序访问"的部分: http://mongoosejs.com/
See the section on "Driver Access" in the docs: http://mongoosejs.com/
基本上,您可以通过执行YourModel.collection
来访问 node-mongodb-native 驱动程序,然后可以insert
或remove
或drop
或任何您需要的内容.
Basically you can get access to the node-mongodb-native driver by doing YourModel.collection
and then you can insert
or remove
or drop
or whatever you need.
没有文档,但是通过这种方法,您可以在这里访问所有内容: https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/collection.js
There's not a doc, but with this approach you'll get access to everything in here: https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/collection.js
在您的情况下,您可能希望跳过在测试套件中使用猫鼬,而使用 node-mongodb-native ,甚至编写一个简单的 mongodb shell脚本,该脚本可以在测试开始之前运行.
In your case you may want to skip using mongoose in your test suite and use the node-mongodb-native directly, or even write a simple mongodb shell script that can be run before your tests start.
这篇关于如何在猫鼬中进行原始mongodb操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!