2015-07-11 63 views
0

我試圖將一堆子文檔推送到文檔的數組中。我究竟做錯了什麼?無法將子文檔推送到Mongoose中的文檔數組中

錯誤和代碼在下面提供。

錯誤:

21:28:49 web.1 | { [CastError: Cast to undefined failed for value "{ url: 'http://www.bbc.co.uk', 
21:28:49 web.1 | _id: 55a17c812424c54413de3332, 
21:28:49 web.1 | entities: [], 
21:28:49 web.1 | terms: [] },{ url: 'http://www.google.com', 
21:28:49 web.1 | _id: 55a17c812424c54413de3333, 
21:28:49 web.1 | entities: [], 
21:28:49 web.1 | terms: [] }" at path "documents"] 
21:28:49 web.1 | stack: 'Error\n at MongooseError.CastError (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/error/cast.js:18:16)\n at SchemaArray.cast (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/schema/array.js:157:15)\n at Query._castUpdateVal (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/query.js:2270:22)\n at Query._walkUpdatePath (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/query.js:2240:25)\n at Query._castUpdate (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/query.js:2118:23)\n at Query.update (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/query.js:1959:22)\n at Function.update (/Users/martynas/Desktop/sandbox/osint-api/node_modules/mongoose/lib/model.js:1734:13)\n at exports.populateCase (/Users/martynas/Desktop/sandbox/osint-api/app/controllers/case.js:69:10)\n at Layer.handle [as handle_request] (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/layer.js:82:5)\n at next (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/route.js:100:13)\n at Route.dispatch (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/route.js:81:3)\n at Layer.handle [as handle_request] (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/layer.js:82:5)\n at /Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/index.js:234:24\n at param (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/index.js:331:14)\n at param (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/index.js:347:14)\n at Function.proto.process_params (/Users/martynas/Desktop/sandbox/osint-api/node_modules/express/lib/router/index.js:391:3)', 
21:28:49 web.1 | message: 'Cast to undefined failed for value "{ url: \'http://www.bbc.co.uk\',\n _id: 55a17c812424c54413de3332,\n entities: [],\n terms: [] },{ url: \'http://www.google.com\',\n _id: 55a17c812424c54413de3333,\n entities: [],\n terms: [] }" at path "documents"', 
21:28:49 web.1 | name: 'CastError', 
21:28:49 web.1 | kind: undefined, 
21:28:49 web.1 | value: [{"url":"http://www.bbc.co.uk","_id":"55a17c812424c54413de3332","entities":[],"terms":[]},{"url":"http://www.google.com","_id":"55a17c812424c54413de3333","entities":[],"terms":[]}], 
21:28:49 web.1 | path: 'documents' } 

案例:

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var Document = require('./document'); 

var CaseSchema = ({ 
    documents: [Document] 
}); 

module.exports = mongoose.model('Case', CaseSchema); 

文件:

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var DocumentSchema = ({ 
    url: String 
}); 

module.exports = mongoose.model('Document', DocumentSchema); 

代碼:

'use strict'; 

var mongoose = require('mongoose'); 
var Case = mongoose.model('Case'); 
var Document = mongoose.model('Document'); 

exports.populateCase = function(req, res) { 

    var caseId = req.params.caseId; 

    var doc1 = new Document({ 
     url: 'http://www.bbc.co.uk' 
    }); 
    var doc2 = new Document({ 
     url: 'http://www.google.com' 
    }); 

    Case.update({_id: caseId}, {$pushAll: { documents: [doc1, doc2] }}, {upsert: true}, function(err) { 
     if (err) { 
      console.log(err); 
     } else { 
      console.log('YAY!'); 
     } 
    }); 
}; 

回答

2

你可以試試這個方法:

案例

var mongoose = require('mongoose'), 
    Schema = mongoose.Schema 
var CaseSchema = ({ 
    documents: [{type: Schema.Types.Object, ref: 'Document' }] 
}); 

var Case = mongoose.model('Case', CaseSchema); 
module.exports = Case 

文件

var mongoose = require('mongoose'), 
    Schema = mongoose.Schema 

var DocumentSchema = ({ 
    url: String 
}); 

module.exports = mongoose.model('Document', DocumentSchema); 

應用

var Document = require('./Document') 
var Case = require('./Case') 

var doc1 = new Document({ 
    url: 'http://www.bbc.co.uk' 
}); 
var doc2 = new Document({ 
    url: 'http://www.google.com' 
}); 


Case.update({_id: caseId}, {$pushAll: { documents: [doc1, doc2] }}, {upsert: true}, function(err) { 
    if (err) { 
     console.log(err); 
    } else { 
     console.log('YAY!'); 
    } 
}); 
+0

這種方式雖然意味着你只存儲文檔的Id,並且爲了訪問這些文檔元素,在它們上做一個'.populate()'。 –

+0

不,它會保存整個對象,因爲類型是「對象」而不是「對象ID」。 – hassansin

+0

糟糕,我沒有注意到你指定了'Object'而不是'ObjectId'。 –

2

子文檔需要一個貓鼬架構,而不是貓鼬模型。您將需要指定

var CaseSchema = ({ 
    documents: [Document.DocumentSchema] 
}); 

,或者你可以把你的case.js文件中的DocumentSchema變量聲明,並使用

var CaseSchema = ({ 
    documents: [DocumentSchema] 
}); 

此外,改變你的case.js您的文檔聲明文件被

var Document = mongoose.model('Document'); 

取決於你如何加載所有的模型文件,您可能需要添加到代碼

require('./document.js'); 
require('./code.js'); 
+0

我想你前往朝着正確的方向:-)但是,我是在使用第一個示例時出現錯誤:TypeError:模式數組路徑「文檔」的無效值。模式聲明需要位於單獨的文件中。我怎樣才能做到這一點? – martynas

+0

我忘了在最後一部分添加。將case.js文件中的Document改爲'var Document = mongoose.model('Document');' –

0

我禁用了「_id」字段,並在案例模型中添加了「String」類型的自定義「_id」字段。在我的解決方案中,它使用舊模板中的db_manager(不需要它)文件來連接數據庫(可以使用默認模板)。

案例:

var mongoose = require('mongoose'); 
var Document = require('./document'); 
var dbManager = require('./../configuration/db_manager.js'); 
var Schema = mongoose.Schema; 

var CaseSchema = new Schema({ 
    _id: String, 
    documents: [Document.schema] 
}, { 
    _id: false 
}); 

exports.schema = CaseSchema; 
exports.model = dbManager.appConnection.model('Case', CaseSchema); 

文件:

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var dbManager = require('./../configuration/db_manager.js'); 

var DocumentSchema = new Schema({ 
    url: String 
}); 

exports.model = dbManager.appConnection.model('Document', DocumentSchema); 
exports.schema = DocumentSchema; 

代碼

exports.populateCase = function(req, res) { 
    var caseId = req.params.caseId; 

    var doc1 = new documentDbModel.model({ 
     url: 'http://www.bbc.co.uk' 
    }); 

    var doc2 = new documentDbModel.model({ 
     url: 'http://www.google.com' 
    }); 

    var docs = [doc1, doc2]; 

    caseDbModel.model.update({ 
    "_id": caseId 
    }, { 
    "$pushAll": { 
     "documents": docs 
    } 
    }, { 
    "upsert": true 
    }, function(err) { 
    if (err) { 
     console.log(err); 
    } else { 
     console.log('YAY!'); 
    } 
    }); 
}; 
相關問題