1

こんにちは、次のコードを使用してmongodbに値を挿入しました

var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://localhost/TruJuncSVC1');
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {

console.log('connection opened in Mongodb with SAMPLE TESTING Database')
var schema = new mongoose.Schema({Response: String,Id:String,Name:String,News:String,Details:String});

var SvcOrch = db.model('SvcOrch', schema);
var data = new SvcOrch({ 'Response': 'DailyLoad','Id':'S3000981','Name':'Stears','News':'Mens Boutiques Try New Tactics for Winning Loyal CustomersTradition Finds New Biz in Old Font','Details':'Stears Roebuck and Co. or Stears, is an American chain of department stores,[2] which was founded by Richard Warren Sears and Alvah Curtis Roebuck in the late 19th century. Formerly a component of the Dow Jones Industrial Average, Stears bought out Kmart[3] in early 2005, creating the Stears Holdings Corporation.'});


                        data.save(function (err) {
                        if (err) return handleError(err);
                        // saved!
                        console.log('\nResponse data saved in MongoDB')
                        })
                        });
                        });

各属性に1つの値を挿入できます.1つの属性に複数の値を保存する必要があります.idには2つまたは3つの値が必要であり、すべての属性に同じことが言えます.さらに先に進む方法についてのアイデアは非常に役立ちます.ありがとうあらかじめ

4

2 に答える 2

2

1 つの属性に複数の値を格納する必要がある場合は、配列として宣言します。

たとえば、newsドキュメントごとに複数のアイテムを保存する場合は、スキーマを次のように変更します。

var schema = new mongoose.Schema({
    Response: String,
    Id: String,
    Name: String,
    News: [String],
    Details: String
});

そして、複数のニュース値を持つ新しいアイテムを次のように作成します。

var data = new SvcOrch({
    Response: 'DailyLoad',
    Id: 'S3000981',
    Name: 'Stears',
    News: ['News item 1', 'News item 2'],
    Details: 'More details...'
});
于 2013-01-02T14:00:20.427 に答える