Meteor - 集合(Collection)

Meteor - 集合(Collection) 首页 / Meteor入门教程 / Meteor - 集合(Collection)

在本章中,无涯教程将学习如何使用 MongoDB 集合。

创建集合

无涯教程可以使用以下代码创建一个新集合-

MyCollection = new Mongo.Collection('myCollection');

新增数据

创建集合后,无涯教程可以使用 insert 方法添加数据。

MyCollection = new Mongo.Collection('myCollection');

var myData = {
   key1: "value 1...",
   key2: "value 2...",
   key3: "value 3...",
   key4: "value 4...",
   key5: "value 5..."
}

MyCollection.insert(myData);

查找数据

无涯教程可以使用 find 方法在集合中搜索数据。

MyCollection = new Mongo.Collection('myCollection');

var myData = {
   key1: "value 1...",
   key2: "value 2...",
   key3: "value 3...",
   key4: "value 4...",
   key5: "value 5..."
}

MyCollection.insert(myData);

var findCollection = MyCollection.find().fetch();
console.log(findCollection);

控制台将显示无涯教程之前插入的数据。

Meteor Collection Find

通过添加搜索参数,无涯教程可以获得相同的输出。

MyCollection = new Mongo.Collection('myCollection');

var myData = {
   key1: "value 1...",
   key2: "value 2...",
   key3: "value 3...",
   key4: "value 4...",
   key5: "value 5..."
}

MyCollection.insert(myData);

var findCollection = MyCollection.find({key1: "value 1..."}).fetch();
console.log(findCollection);

更新数据

下一步是更新无涯教程的数据。创建集合并插入新数据后,可以使用 update 方法。

MyCollection = new Mongo.Collection('myCollection');

var myData = {
   key1: "value 1...",
   key2: "value 2...",
   key3: "value 3...",
   key4: "value 4...",
   key5: "value 5..."
}

MyCollection.insert(myData);

var findCollection = MyCollection.find().fetch();
var myId = findCollection[0]._id;

var updatedData = {
   key1: "updated value 1...",
   key2: "updated value 2...",
   key3: "updated value 3...",
   key4: "updated value 4...",
   key5: "updated value 5..."
}

MyCollection.update(myId, updatedData);

var findUpdatedCollection = MyCollection.find().fetch();
console.log(findUpdatedCollection);

控制台将显示无涯教程的集合已更新。

Meteor Collections Update

删除数据

可以使用删除方法从集合中删除数据。在此示例中,无涯教程将 id 设置为删除特定数据的参数。

MyCollection = new Mongo.Collection('myCollection');

var myData = {
   key1: "value 1...",
   key2: "value 2...",
   key3: "value 3...",
   key4: "value 4...",
   key5: "value 5..."
}

MyCollection.insert(myData);

var findCollection = MyCollection.find().fetch();
var myId = findCollection[0]._id;

MyCollection.remove(myId);

var findDeletedCollection = MyCollection.find().fetch();
console.log(findDeletedCollection);

控制台将显示一个空数组。

Meteor Collections Remove

如果要删除集合中的所有内容,可以使用相同的方法,但是,无涯教程将使用空对象 {} 代替 id 。出于安全原因,无涯教程需要在服务器上执行此操作。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/meteor/meteor-collections.html

来源:LearnFk无涯教程网

if (Meteor.isServer) {

   MyCollection = new Mongo.Collection('myCollection');

   var myData = {
      key1: "value 1...",
      key2: "value 2...",
      key3: "value 3...",
      key4: "value 4...",
      key5: "value 5..."
   }

   MyCollection.insert(myData);
   MyCollection.remove({});
	
   var findDeletedCollection = MyCollection.find().fetch();
   console.log(findDeletedCollection);
}

无涯教程还可以使用其他参数删除数据。与前面的示例一样,Meteor将迫使无涯教程从服务器执行此操作。

if (Meteor.isServer) {

   MyCollection = new Mongo.Collection('myCollection');

   var myData = {
      key1: "value 1...",
      key2: "value 2...",
      key3: "value 3...",
      key4: "value 4...",
      key5: "value 5..."
   }

   MyCollection.insert(myData);
   MyCollection.remove({key1: "value 1..."});
	
   var findDeletedCollection = MyCollection.find().fetch();
   console.log(findDeletedCollection);
}

可以看出,该数据已从命令窗口中删除。

Meteor Collections Remove Server

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

持续交付36讲 -〔王潇俊〕

快速上手Kotlin开发 -〔张涛〕

iOS开发高手课 -〔戴铭〕

Vim 实用技巧必知必会 -〔吴咏炜〕

Spring编程常见错误50例 -〔傅健〕

讲好故事 -〔涵柏〕

超级访谈:对话张雪峰 -〔张雪峰〕

朱涛 · Kotlin编程第一课 -〔朱涛〕

Kubernetes入门实战课 -〔罗剑锋〕

好记忆不如烂笔头。留下您的足迹吧 :)