2016-03-15 16 views
8

Bu yüzden, MEAN yığında yeniyim ve MongoDB'yi tohumlamaya çalışan bir duvara çarptım. Ben veritabanı ile iletişim kurmak için Mongoose kullanıyorum ve nüfuslu JSON dosyaları kullanarak tohum edebilmek gerektiğini gösteren bir sürü belge var.Düğüm/MongoDB uygulamasını yerleştirmenin en iyi yöntemi nedir?

denedim Ne:

node-mongo-seed; Oldukça düz ileri, ancak sürekli dizilerin sonunda hataları atar. (Belki de eksik BSON modülü hatalı olduğunu?)

{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' } 
js-bson: Failed to load c++ bson extension, using pure JS version 
Seeding files from directory /Users/Antwisted/code/wdi/MEAN/seeds 
---------------------- 
Seeding collection locations 
err = [SyntaxError: /Users/Antwisted/code/wdi/MEAN/seeds/locations.json: Unexpected token {] 

mongoose-seed; Ayrıca oldukça basittir, temel olarak JSON nesnelerini veritabanına dışa aktarmadan önce bir değişkene koyar. Umut verici, ama ... daha fazla hata ...

Successfully initialized mongoose-seed 
[ 'app/models/locationsModel.js' ] 
Locations collection cleared 
Error creating document [0] of Location model 
Error: Location validation failed 
Error creating document [1] of Location model 
Error: Location validation failed 
Error creating document [2] of Location model 
Error: Location validation failed... 

Yani, benim düşüncelerim muhtemelen JSON yapısı içinde bir sözdizimi hatası, ama bu uğraşırken herhangi bir gerçek çözümler vermiştir olmadığını idi (ya da belki Onu özlüyorum? Benim JSON Örnek:

{ 
    { 
     "header": "Dan's Place", 
     "rating": 3, 
     "address": "125 High Street, New York, 10001", 
     "cord1": -73.0812, 
     "cord2": 40.8732, 
     "attributes": ["Hot drinks", "Food", "Premium wifi"], 
     "hours": [ 
      { 
       "days": "Monday - Friday", 
       "hours": "7:00am - 7:00pm", 
       "closed": false 
      }, 
      { 
       "days": "Saturday", 
       "hours": "8:00am - 5:00pm", 
       "closed": false 
      }, 
      { 
       "days": "Sunday", 
       "closed": true 
      } 
     ], 
     "reviews": [ 
      { 
       "rating": 4, 
       "id": ObjectId(), 
       "author": "Philly B.", 
       "timestamp": "new Date('Feb 3, 2016')", 
       "body": "It was fine, but coffee was a bit dull. Nice atmosphere." 
      }, 
      { 
       "rating": 3, 
       "id": ObjectId(), 
       "author": "Tom B.", 
       "timestamp": "new Date('Feb 23, 2016')", 
       "body": "I asked for her number. She said no." 
      } 
     ] 
    }, 
    { 
     "header": "Jared's Jive", 
     "rating": 5, 
     "address": "747 Fly Court, New York, 10001", 
     "cord1": -73.0812, 
     "cord2": 40.8732, 
     "attributes": ["Live Music", "Rooftop Bar", "2 Floors"], 
     "hours": [ 
      { 
       "days": "Monday - Friday", 
       "hours": "7:00am - 7:00pm", 
       "closed": false 
      }, 
      { 
       "days": "Saturday", 
       "hours": "8:00am - 5:00pm", 
       "closed": false 
      }, 
      { 
       "days": "Sunday", 
       "closed": true 
      } 
     ], 
     "reviews": [ 
      { 
       "rating": 5, 
       "id": ObjectId(), 
       "author": "Jacob G.", 
       "timestamp": "new Date('Feb 3, 2016')", 
       "body": "Whoa! The music here is wicked good. Definitely going again." 
      }, 
      { 
       "rating": 4, 
       "id": ObjectId(), 
       "author": "Tom B.", 
       "timestamp": "new Date('Feb 23, 2016')", 
       "body": "I asked to play her a tune. She said no." 
      } 
     ] 
    } 
} 

Ayrıca, (Ben tohumlama işlemi ilk etapta düzgün çalışması için alabilirsiniz varsayarak) JSON içinde alt belgeleri nasıl belirtileceği tamamen emin değilim. Bu sorunları gezinmek konusunda herhangi bir fikir büyük takdir

var mongoose = require('mongoose'); 

var subHoursSchema = new mongoose.Schema({ 
    days: {type: String, required: true}, 
    opening: String, 
    closing: String, 
    closed: {type: Boolean, required: true} 
}); 

var subReviewsSchema = new mongoose.Schema({ 
    rating: {type: Number, required: true, min: 0, max: 5}, 
    author: String, 
    timestamp: {type: Date, "default": Date.now}, 
    body: String 
}); 

var locationSchema = new mongoose.Schema({ 
    name: {type: String, required: true}, 
    address: String, 
    rating: {type: Number, "default": 0, min: 0, max: 5}, 
    attributes: [String], 
    coordinates: {type: [Number], index: '2dsphere'}, 
    openHours: [subHoursSchema], 
    reviews: [subReviewsSchema] 
}); 

mongoose.model('Location', locationSchema); 

:

İşte benim modeli. Teşekkürler!

cevap

9

İstediğin kadar çalıştırmadan önce çalışan olmak için bir mongod örneğin, It belirtilen bir MongoDB Örneği & Koleksiyonu içine bir JSON dosyası yükleyecek mongoimport

kullanarak CLI MongoDB doldurabilirsiniz.

mongoimport ürününü kullanarak bir walkthrough kullanıma sahiptir.

+0

Bu kesinlikle beni başarılmasına yardımcı ne en azından kısmen bakıyordu. Süreci (henüz) alt belgeleri (büyük olasılıkla 'mongodump') içerecek şekilde nasıl değiştireceğinden emin değilim, ancak bu doğru yönde büyük bir adımdı. Teşekkür ederim! – Antwisted

+0

Ne demek, alt belgeleri de dahil ederseniz? – peteb

+0

[Bu gönderi] (http://stackoverflow.com/questions/29354858/create-id-on-subdocuments-on-mongoimport-jsonarray) 'mongoimport' ile alt-dokümanlar konusunda size yardımcı olabilir – peteb

1

JSON şemanızı akmıyor.

bu şekilde JSON Fix:

{ 
    { 
     "name": "Dan's Place", 
     "rating": 3, 
     "address": "125 High Street, New York, 10001", 
     "coordinates": [-73.0812, 40.8732], 
     "attributes": ["Hot drinks", "Food", "Premium wifi"], 
     "openHours": [ 
      { 
       "days": "Monday - Friday", 
       "opening": "7:00am", 
       "closing": "7:00pm", 
       "closed": false 
      }, 
      { 
       "days": "Saturday", 
       "opening": "8:00am", 
       "closing": "5:00pm", 
       "closed": false 
      }, 
      { 
       "days": "Sunday", 
       "closed": true 
      } 
     ], 
     "reviews": [ 
      { 
       "rating": 4, 
       "author": "Philly B.", 
       "timestamp": "new Date('Feb 3, 2016')", 
       "body": "It was fine, but coffee was a bit dull. Nice atmosphere." 
      }, 
      { 
       "rating": 3, 
       "author": "Tom B.", 
       "timestamp": "new Date('Feb 23, 2016')", 
       "body": "I asked for her number. She said no." 
      } 
     ] 
    }, 
    { 
     "name": "Jared's Jive", 
     "rating": 5, 
     "address": "747 Fly Court, New York, 10001", 
     "coordinates": [-73.0812, 40.8732], 
     "attributes": ["Live Music", "Rooftop Bar", "2 Floors"], 
     "openHours": [ 
      { 
       "days": "Monday - Friday", 
       "opening": "7:00am", 
       "closing": "7:00pm", 
       "closed": false 
      }, 
      { 
       "days": "Saturday", 
       "opening": "8:00am", 
       "closing": "5:00pm", 
       "closed": false 
      }, 
      { 
       "days": "Sunday", 
       "closed": true 
      } 
     ], 
     "reviews": [ 
      { 
       "rating": 5, 
       "author": "Jacob G.", 
       "timestamp": "new Date('Feb 3, 2016')", 
       "body": "Whoa! The music here is wicked good. Definitely going again." 
      }, 
      { 
       "rating": 4, 
       "author": "Tom B.", 
       "timestamp": "new Date('Feb 23, 2016')", 
       "body": "I asked to play her a tune. She said no." 
      } 
     ] 
    } 
} 

şununla firavunfaresi modellerini etkileşim olduğunu kendi tohum senaryo yazmaya firavunfaresi-veri-tohum kullanabilirsiniz: https://github.com/sharvit/mongoose-data-seed

İlgili konular