node.js 如何使用 Mongoose 将 json 导入 MongoDB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30696946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import json into MongoDB using Mongoose
提问by dhj
I have a few problems with this, which is whats making it tricky, so...
我对此有一些问题,这就是让它变得棘手的原因,所以......
I am using Mongoose and MongoLab, I can store data and retrieve it just fine, but I want a system that allows me to do a base seed of the database.
我正在使用 Mongoose 和 MongoLab,我可以很好地存储和检索数据,但我想要一个允许我做数据库基本种子的系统。
I have the schemas created for the collections, but none are ran because there is no data, so I can't seem to run a normal mongoimport as the collection isn't yet created.
我为集合创建了模式,但没有运行,因为没有数据,所以我似乎无法运行正常的 mongoimport,因为尚未创建集合。
I want to add something to my node server so that if the collection doesn't exist or is empty, it loads a schema for a collection and then inserts the json for the seed data.
我想向我的节点服务器添加一些内容,以便如果集合不存在或为空,它会加载集合的架构,然后为种子数据插入 json。
so I have this...
所以我有这个...
var Club = require('./schemas/Club');
I normally use Club.find, or Club.save etc, thats working fine.
我通常使用 Club.find 或 Club.save 等,这很好用。
I want to just run a save against an array of Objects to the Club collection which it needs to create.
我只想针对需要创建的 Club 集合的对象数组运行保存。
I did look into mongoose-fixture but its not been updated in years, and there is probably a way of doing this without needing so much extra code, as I have the schema defined, and the array of json ready.
我确实研究过 mongoose-fixture,但它已经好几年没有更新了,而且可能有一种不需要太多额外代码的方法,因为我已经定义了架构,并且准备好了 json 数组。
This is the success event I listed for when I guess I want to do the check and import.
这是我列出的成功事件,当我想我想做检查和导入时。
mongoose.connection.on('open', function () {
console.log('mongoose.connection.opened');
});
Also, to consider, if I wanted to create two collections, and when it generates the ObjectId() for the items in the first collection, I can imagine wanting to use those in the second collection as a ref.
另外,考虑一下,如果我想创建两个集合,并且当它为第一个集合中的项目生成 ObjectId() 时,我可以想象想要使用第二个集合中的那些作为参考。
Just assume Club objects only have one string property for now.
假设 Club 对象现在只有一个字符串属性。
// contents of data/club.json
[
{ 'name' : 'Barcelona' },
{ 'name' : 'Real Madrid' },
{ 'name' : 'Valencia' }
]
Any help much appreciated
非常感谢任何帮助
回答by Sylvain Leroux
If I understand it well, all you need is to upload a JSON document to your MongoDB collection from Mongoose. Given that your model is named Club, you can access raw driver methods through Club.collection. And use insertManyto achieve what you want.
如果我理解得很好,您只需要从 Mongoose 将 JSON 文档上传到您的 MongoDB 集合。鉴于您的模型已命名Club,您可以通过 访问原始驱动程序方法Club.collection。并用于insertMany实现您想要的。
Here is a stand alone example (the interesting stuff is at the end):
这是一个独立的例子(有趣的东西在最后):
> var mongoose = require('mongoose')
> var assert = require('assert')
> mongoose.connect('mongodb://localhost/test');
> var Schema = mongoose.Schema
> var clubSchema = new Schema({
... name: String,
... })
> var Club = mongoose.model('Club', clubSchema)
// Now, the interesting part:
> data = [
... { 'name' : 'Barcelona' },
... { 'name' : 'Real Madrid' },
... { 'name' : 'Valencia' }
... ]
> Club.collection.insertMany(data, function(err,r) {
... assert.equal(null, err);
... assert.equal(3, r.insertedCount);
...
... db.close();
... })
And check from the Mongo Shell:
并从 Mongo Shell 检查:
> db.clubs.find()
{ "_id" : ObjectId("5574b464b680174d79e37601"), "name" : "Barcelona" }
{ "_id" : ObjectId("5574b464b680174d79e37602"), "name" : "Real Madrid" }
{ "_id" : ObjectId("5574b464b680174d79e37603"), "name" : "Valencia" }
回答by sstruct
Sometimes shell scripts may help:
有时 shell 脚本可能会有所帮助:
for filename in *; do mongoimport -d mydb -c $filename --jsonArray done
See also: MongoDB Bulk import using mongoimport from Windows folder

