gpt4 book ai didi

node.js - 使用nodejs将多个值插入mongodb中的属性

转载 作者:太空宇宙 更新时间:2023-11-04 01:13:18 25 4
gpt4 key购买 nike

您好,我使用以下代码将值插入到我的 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')
})
});
});

我能够为每个属性插入一个值,我需要为单个属性保存多个值,比如 id 应该有 2 或 3 个值,所有属性都相同。任何关于如何进一步进行的想法都会很多有帮助。提前致谢

最佳答案

如果需要存储单个属性的多个值,请将其声明为数组。

例如,如果您想在每个文档中存储多个 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...'
});

关于node.js - 使用nodejs将多个值插入mongodb中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14117545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com