【发布时间】:2014-06-25 02:24:39
【问题描述】:
基本上,我想在名为“Events”的 Mongodb 集合中插入一个名为“Events”的对象数组,其中每个条目都有一个 ID。
这是我想在 json 中得到的结果:
{
"events" : [
{
"_id" : ObjectId("53a99cc608ad49712a830081"),
"eTitle" : "Freshers Fair",
"eDesc" : "Bring some friends with you oh wait, you have non ha !",
"eDate" : "2014-06-19 11:20",
"eLink" : "http://fair.com",
"eEvent" : "NEFS"
},
{
"_id" : ObjectId("53a99cc608ad49712a830082"),
"eTitle" : "Blahh",
"eDesc" : "Blah fdinidf !",
"eDate" : "2014-06-19 11:20",
"eLink" : "http://google.com",
"eEvent" : "NEFS"
}
]
}
到目前为止,这是我得到的结果:
[
{
"_id":"53a9b5ed745363432d823d7a",
"eTitle":"jrnfdeiujn rd",
"eDesc":"grfdsreds",
"eDate":"2014-07-05 22:33",
"eLink":"reser",
"eEvent":"Victoria Center"
},
{
"_id":"53a9b771745363432d823d7b",
"eTitle":"Hello worlds",
"eDesc":"blah",
"eDate":"2014-07-20 22:33",
"eLink":"http://google.com",
"eEvent":"social"
}
]
这就是我使用 node.js 插入数据的方式:
// Set our collection
var collection = db.get('Events');
// Submit to the DB
collection.insert({
"eTitle" : eTitle,
"eDesc" : eDesc,
"eDate" : eDate,
"eLink" : eLink,
"eEvent" : eEvent
}, function (err, doc) {
if (err) {
// If it failed, return error
res.send("There was a problem adding the information to the database.");
}
else {
// If it worked, set the header so the address bar doesn't still say /adduser
res.location("eventlist");
// And forward to success page
res.redirect("eventlist");
}
});
请问我如何使格式看起来是我提供的第一个 json 格式。抱歉这个nooby问题,刚开始学习节点!非常感谢
更新
发布活动:
router.get('/eventlist', function(req, res) {
var db = req.db;
var collection = db.get('Events');
collection.find({},{},function(e,docs){
console.log(docs);
res.send(docs);
// res.render('eventlist', {docs: JSON.stringify(docs), title: 'Test'});
});
});
【问题讨论】:
-
不是已经这样了吗?您当前的 JSON 位于名为
Events\ 的集合中 -
@tymeJV 事情是我目前正在 ios 中加载 json 对象,格式必须看起来像第一个。 :(
-
您的
Events集合不会将集合名称作为第一个属性返回 - 为什么不在获取数据时手动添加它?var events = { Events: data } -
是的,这正是我正在寻找的。请问我在哪里添加? @tymeJV
-
发布您的电话以获取所有事件并发布答案
标签: json node.js mongodb express