rei*_*122 9 javascript mongodb meteor mongodb-query
我在Meteor 1.1.0.2应用程序中有以下(简化的)SimpleSchema架构:
Tickers.attachSchema(
new SimpleSchema({
entries: {
type: [TickerEntries],
defaultValue: [],
optional: true
}
})
);
TickerEntries = new SimpleSchema({
id: {
type: String,
autoform: {
type: "hidden",
label: false,
readonly: true
},
optional: true,
autoValue: function () {
if (!this.isSet) {
return new Mongo.Collection.ObjectID()._str;
}
}
},
text: {
type: String,
label: 'Text'
}
};
Run Code Online (Sandbox Code Playgroud)
在数据库中,我有以下条目:
{
"_id" : "ZcEvq9viGQ3uQ3QnT",
"entries" : [
{
"text" : "a",
"id" : "fc29774dadd7b37ee0dc5e3e"
},
{
"text" : "b",
"id" : "8171c4dbcc71052a8c6a38fb"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想删除ID指定的条目数组中的一个条目.
如果我在meteor-mongodb-shell中执行以下命令,它可以正常工作:
db.Tickers.update({_id:"3TKgHKkGnzgfwqYHY"}, {"$pull":{"entries": {"id":"8171c4dbcc71052a8c6a38fb"}}})
Run Code Online (Sandbox Code Playgroud)
但问题是,如果我要在Meteor中做同样的事情,它就行不通.这是我的代码:
Tickers.update({id: '3TKgHKkGnzgfwqYHY'}, {$pull: {'entries': {'id': '8171c4dbcc71052a8c6a38fb'}}});
Run Code Online (Sandbox Code Playgroud)
我也尝试过以下方法:
Tickers.update('3TKgHKkGnzgfwqYHY', {$pull: {'entries': {'id': '8171c4dbcc71052a8c6a38fb'}}});
Run Code Online (Sandbox Code Playgroud)
这些命令都没有给我一个错误,但它们不会从我的文档中删除任何内容.
是否可能,该$pull命令未得到正确支持或我在某处犯了错误?
提前致谢!
编辑:我发现了问题,这在我的描述中无法看到,因为我简化了我的架构.在我的应用程序有一个附加属性timestamp中TickerEntries:
TickerEntries = new SimpleSchema({
id: {
type: String,
optional: true,
autoValue: function () {
if (!this.isSet) {
return new Mongo.Collection.ObjectID()._str;
}
}
},
timestamp: {
type: Date,
label: 'Minute',
optional: true,
autoValue: function () {
if (!this.isSet) { // this check here is necessary!
return new Date();
}
}
},
text: {
type: String,
label: 'Text'
}
});
Run Code Online (Sandbox Code Playgroud)
感谢Kyll的提示,我创建了一个Meteorpad并发现,该autovalue功能导致了问题.
我现在已将函数更改为以下代码:
autoValue: function () {
if (!this.isSet && this.operator !== "$pull") { // this check here is necessary!
return new Date();
}
}
Run Code Online (Sandbox Code Playgroud)
而现在它正在发挥作用.看来,在拉动项目/对象的情况下返回autovalue值,它会取消拉取操作,因为该值未设置为返回值(因此timestamp属性保留旧值但不拉).
以下是相应的Meteorpad测试它(只需在autovalue功能中注释掉操作员的检查):http://meteorpad.com/pad/LLC3qeph66pAEFsrB/Leaderboard
谢谢大家的帮助,你的所有帖子都对我很有帮助!
对于基本的流星应用程序,我称之为"bunk".如果您创建一个全新的项目并只是定义集合,那么$pull运算符将按预期工作:
安慰:
meteor create tickets
cd tickets
meteor run
Run Code Online (Sandbox Code Playgroud)
然后打开一个shell并插入你的数据:
meteor mongo
> db.tickets.insert(data) // exactly your data in the question
Run Code Online (Sandbox Code Playgroud)
然后只生成一些基本代码和模板:
tickers.js
Tickers = new Meteor.Collection("tickers");
if (Meteor.isClient) {
Template.body.helpers({
"tickers": function() {
return Tickers.find({});
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
Run Code Online (Sandbox Code Playgroud)
tickers.html
<head>
<title>tickers</title>
</head>
<body>
<h1>Welcome to Meteor!</h1>
<ul>
{{#each tickers}}
{{> ticker}}
{{/each}}
</ul>
</body>
<template name="ticker">
<li>
{{_id}}
<ul>
{{#each entries}}
{{> entry }}
{{/each}}
</ul>
</li>
</template>
<template name="entry">
<li>{{ id }} - {{text}}</li>
</template>
Run Code Online (Sandbox Code Playgroud)
应用程序运行正常,因此在浏览器控制台中执行.update()(缩进读取):
Tickers.update(
{ "_id": "ZcEvq9viGQ3uQ3QnT" },
{ "$pull": { "entries": { "id": "fc29774dadd7b37ee0dc5e3e" } }}
)
Run Code Online (Sandbox Code Playgroud)
并且该条目将从条目中删除,并且页面将在没有该项目的情况下刷新.所以一切都消失了,就像预期一样.
即使添加SimpleSchema和Collection2包,也没有区别:
meteor add aldeed:simple-schema
meteor add aldeed:collection2
Run Code Online (Sandbox Code Playgroud)
tickers.js
Tickers = new Meteor.Collection("tickers");
TickerEntries = new SimpleSchema({
"id": {
type: String,
optional: true,
autoValue: function() {
if (!this.isSet) {
return new Mongo.Collection.ObjectID()._str
}
}
},
"text": {
type: String
}
});
Tickers.attachSchema(
new SimpleSchema({
entries: { type: [TickerEntries] }
})
);
if (Meteor.isClient) {
Template.body.helpers({
"tickers": function() {
return Tickers.find({});
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
Run Code Online (Sandbox Code Playgroud)
重新初始化数据并在浏览器控制台中运行相同的命令,一切都保持不变.
检查您自己的操作中的这个或任何输入错误或其他差异,以找出为什么这对您不起作用的线索.
我强烈建议这样做,因为"像这样开始新鲜"会显示预期的行为,如果你看到不同的行为,那么你可能已经安装了另一个插件的问题.
但一般来说,这是有效的.
| 归档时间: |
|
| 查看次数: |
1011 次 |
| 最近记录: |