我正在尝试向存储在MongoDB集合中的每个文档添加一个包含Array的附加属性。
var mongoose = require('mongoose');
var artistSchema = mongoose.Schema({
name: String,
city: String,
imageFile: String,
tracks:Array <-- This is the new property I'd like to add to each doc
});
// define an Artist model with this mongoose instance
mongoose.model('Artist', artistSchema);
// create a new connection
var conn = mongoose.createConnection('mongodb://localhost/treefort');
//@param artist - Document to update
//@param tracks - Array to add to the document
function updateArtistWithTracks(artist, tracks, callback){
var conditions = { name: artist.name }
, update = {tracks : tracks}
, options = { multi: true };
Artist.update(conditions, update, options, callback);
}
使用Artist.findOne()找到的每个文档都调用'updateArtistWithTracks',即
function findArtistByName(artistName, callback){
Artist.findOne({name: artistName}, function (err, art) {
if(err) throw 'Could Not Find Arist With Name: ' + artistName;
callback(art);
});
};
从更新函数调用的回调不包含任何错误,并且影响文档的数量总是1:
Artist.update(conditions, update, options, callback); //callback is called with (null, 1) each time.
当我查询我的mongodb时,所有文件都保持不变。有人可以帮忙吗?
更新
'tracks'是一个对象数组。这就是它的样子:
"Tracks": [
{
"title": "Dance of jesters part 1 Oct. 10",
"duration": "98466",
"stream_url": "http://api.soundcloud.com/tracks/25087329/stream"
},
{
"title": "Little french suite mov.3 Oct. 10",
"duration": "96401",
"stream_url": "http://api.soundcloud.com/tracks/25087293/stream"
}]
是不是因为我正在尝试添加我的更新失败的子文档?
再次更新
这是我的完整代码。我已经简化了一些代码。包含一组对象的新“轨道”属性仍然不会添加到文档中!?
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/treefort');
var db = mongoose.connection;
var dbConnectionOpen = false;
var artistSchema = mongoose.Schema({
name: String,
city: String,
imageFile: String,
tracks: [
{
"title": String,
"duration": String,
"stream_url": String
}
]
});
var Artist = mongoose.model('Artist', artistSchema);
/** Mongo Connection Callbacks**/
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback() {
dbConnectionOpen = true;
start();
});
var trackData;
function start() {
//Read Track JSON File
var fs = require('fs');
var file = '/Users/nick/DropBox/artist-tracks.json';
fs.readFile(file, 'utf8', function (err, data) {
if (err) throw 'Could Not read source file';
trackData = JSON.parse(data);
runUpdate();
});
}
function runUpdate() {
var currentArtist = trackData.shift();
if (currentArtist) {
Artist.findOne({ name: currentArtist.Name }, function (err, artDoc) {
if (err) throw 'Could Not Find Artist With Name: ' + currentArtist.Name;
if (artDoc) {
Artist.update(
{ _id: artDoc._id},
{$push: {"tracks": {title: currentArtist.Tracks[0].title, duration: currentArtist.Tracks[0].duration, stream_url:currentArtist.Tracks[0].stream_url }}},
{ upsert: true },
function (err, numberAffected, raw) {
if (err) console.log(err);
});
}
if (trackData) {
console.log(trackData.length)
runUpdate();
}
if(trackData.length === 0){
process.exit(0);
}
});
}
}