修改示例(待办事项)

时间:2013-10-16 18:43:10

标签: mongodb meteor

试图深入了解流星并将其与mongoDB结合起来我想问一个简单的问题。

以下是示例提供的示例,用于向DB填充信息(如果它为空)。 我所要做的就是在开始时添加一行代码,在开头就像Lists.remove();一样。简单地说,我可以继续使用该示例,能够重新启动流星,重置数据库中的信息。我只是不确定数据集究竟是什么。我知道一个简单的命令,就像我上面列出的那个,我可以做到这一点。我今天第一次看到mongodb和meteor,所以感谢宽容:)

// if the database is empty on server start, create some sample data.
Meteor.startup(function () {
if (Lists.find().count() === 0) {
var data = [
  {name: "Meteor Principles",
   contents: [
     ["Data on the Wire", "Simplicity", "Better UX", "Fun"],
     ["One Language", "Simplicity", "Fun"],
     ["Database Everywhere", "Simplicity"],
     ["Latency Compensation", "Better UX"],
     ["Full Stack Reactivity", "Better UX", "Fun"],
     ["Embrace the Ecosystem", "Fun"],
     ["Simplicity Equals Productivity", "Simplicity", "Fun"]
   ]
  },
  {name: "Languages",
   contents: [
     ["Lisp", "GC"],
     ["C", "Linked"],
     ["C++", "Objects", "Linked"],
     ["Python", "GC", "Objects"],
     ["Ruby", "GC", "Objects"],
     ["JavaScript", "GC", "Objects"],
     ["Scala", "GC", "Objects"],
     ["Erlang", "GC"],
     ["6502 Assembly", "Linked"]
     ]
  },
  {name: "Favorite Scientists",
   contents: [
     ["Ada Lovelace", "Computer Science"],
     ["Grace Hopper", "Computer Science"],
     ["Marie Curie", "Physics", "Chemistry"],
     ["Carl Friedrich Gauss", "Math", "Physics"],
     ["Nikola Tesla", "Physics"],
     ["Claude Shannon", "Math", "Computer Science"]
   ]
  }
];

var timestamp = (new Date()).getTime();
for (var i = 0; i < data.length; i++) {
  var list_id = Lists.insert({name: data[i].name});
  for (var j = 0; j < data[i].contents.length; j++) {
    var info = data[i].contents[j];
    Todos.insert({list_id: list_id,
                  text: info[0],
                  timestamp: timestamp,
                  tags: info.slice(1)});
    timestamp += 1; // ensure unique timestamp.
  }
}}});

2 个答案:

答案 0 :(得分:3)

Lists.remove()不起作用,因为你必须在里面指定一些东西,甚至什么都不是。 Lists.remove({})就是你想要的。

正如文档http://docs.meteor.com/#remove中所述:

  

作为安全措施,如果省略选择器(或未定义),则不会删除任何文档。如果您确实要从集合中删除所有文档,请将选择器设置为{}。

所以:

Meteor.startup(function () {
  Lists.remove({});
  if (Lists.find().count() === 0) {
    console.log("Lists was empty");
    ...

注意:您将在启动流星的终端窗口中看到console.log,而不是浏览器控制台。

答案 1 :(得分:1)

您可以在erases您的mongodb数据的命令行中使用meteor reset