在查询性能方面有点奇怪...我需要运行一个查询来完成文档的总计数,并且还可以返回一个可以限制和偏移的结果集.
所以,我总共有57个文档,用户希望10个文档偏移20个.
我可以想到两种方法,首先查询所有57个文档(作为数组返回),然后使用array.slice返回他们想要的文档.第二个选项是运行2个查询,第一个使用mongo的本机'count'方法,然后使用mongo的本机$ limit和$ skip聚合器运行第二个查询.
您认为哪个会更好地扩展?在一个查询中完成所有操作,还是运行两个单独的查询?
编辑:
// 1 query
var limit = 10;
var offset = 20;
Animals.find({}, function (err, animals) {
if (err) {
return next(err);
}
res.send({count: animals.length, animals: animals.slice(offset, limit + offset)});
});
// 2 queries
Animals.find({}, {limit:10, skip:20} function (err, animals) {
if (err) {
return next(err);
}
Animals.count({}, function (err, count) {
if (err) {
return next(err);
}
res.send({count: count, animals: animals});
});
});
Run Code Online (Sandbox Code Playgroud) Mongoose在Schema中添加了一个'__v'属性用于版本控制 - 是否可以全局禁用它或全局隐藏所有查询?
我有一个简单的形式:
<form name="add-form" data-ng-submit="addToDo()">
<label for="todo-name">Add a new item:</label>
<input type="text" data-ng-model="toDoName" id="todo-name" name="todo-name" required>
<button type="submit">Add</button>
</form>
Run Code Online (Sandbox Code Playgroud)
我的控制器如下:
$scope.addToDo = function() {
if ($scope.toDoName !== "") {
$scope.toDos.push(createToDo($scope.toDoName));
}
}
Run Code Online (Sandbox Code Playgroud)
我想做的是在提交后清除文本输入,所以我只需清除模型值:
$scope.addToDo = function() {
if ($scope.toDoName !== "") {
$scope.toDos.push(createToDo($scope.toDoName));
$scope.toDoName = "";
}
}
Run Code Online (Sandbox Code Playgroud)
除了现在,因为表单输入是"必需的",我在表单输入周围得到一个红色边框.这是正确的行为,但不是我想要的在这种情况下......所以我想清除输入然后模糊输入元素.这导致我:
$scope.addToDo = function() {
if ($scope.toDoName !== "") {
$scope.toDos.push(createToDo($scope.toDoName));
$scope.toDoName = "";
$window.document.getElementById('todo-name').blur();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我知道在Angular文档中对这样的控制器中的DOM进行修改是不受欢迎的 - 但是有更多的Angular方法吗?
我正在学习Express - 并且正在考虑保存配置样式数据的最佳位置.可用选项在app.locals或app.set(设置)中......所以:
app.locals({ config: {
name: 'My App',
domain: 'myapp.com',
viewPath: __dirname+'/views',
viewEngine: 'jade'
port: 3000
} });
app.set('view engine', app.locals.config.viewEngine || 'jade');
Run Code Online (Sandbox Code Playgroud)
这也允许我在我的观点中使用以下内容:
<title>#{config.name}</title> // <title>My App</title>
Run Code Online (Sandbox Code Playgroud)
或者替代方法是使用app.set,如下所示:
app.set('name', 'My App');
app.set('domain', 'myapp.com');
Run Code Online (Sandbox Code Playgroud)
...然后在视图中使用它:
<title>#{settings.name}</title>
Run Code Online (Sandbox Code Playgroud)
我知道这两种方法都有效,但我很难确定哪种方法更好用.目前我倾向于使用app.locals,使用额外的'app'命名空间,因为我相信如果使用app.set,与未来更新和其他模块冲突的可能性会更小.
我刚刚开始使用Angular,所以这可能是一个常见的新手错误,但我正在尝试实现以下url格式:
http://myapp.localhost/index.html#!/about
Run Code Online (Sandbox Code Playgroud)
我认为应该是Angular的默认值?
这是我的配置:
angular.module('App', []).config(function($routeProvider, $locationProvider, VIEWS_ROOT) {
$locationProvider.html5Mode(false);
$locationProvider.hashPrefix = '!';
// Routing
$routeProvider.when('/', {templateUrl: './welcome.html', controller: 'WelcomeController'});
$routeProvider.when('/about', {templateUrl: './about.html', controller: 'AboutController'});
})
.run(function($rootScope) {
//...
});
Run Code Online (Sandbox Code Playgroud)
在我的HTML中,我有一个简单的锚,如下所示:
<a href="#!/about">About</a>
Run Code Online (Sandbox Code Playgroud)
但是,当我单击该锚点时,生成的结果URL是:
http://myapp.localhost/index.html#/!/about
Run Code Online (Sandbox Code Playgroud)
这显然无法匹配我的任何路线...有点难以理解这里发生的事情,或者我做错了什么.我在vhost下运行我的本地Apache实例.mod_rewrite没有任何进展 - 所以看起来Angular正在这样做.
尝试理解传递给setValue时'onlySelf'参数的作用.
this.form.get('name').setValue('', { onlySelf: true })
Run Code Online (Sandbox Code Playgroud)
文档说:"如果onlySelf为true,则此更改只会影响此FormControl的验证,而不会影响其父组件.默认为false."
但是我很难理解这一点.使用Angulars的模型驱动形式仍然相当新.
我很有棱角,所以请耐心等待.前几天我正在阅读一篇文章/文档,强调了在应用程序中构建模块的最佳方法,并且只能松散地记住它.
App.controllers
App.services
....
angular.module('App', [App.controllers, App.services ...);
Run Code Online (Sandbox Code Playgroud)
此代码示例很可能不正确,但重点是将控制器,服务等组合在一个命名空间中.
任何人都可以扩展这种方法吗?
我有以下组件:
export enum Tags {
button = 'button',
a = 'a',
input = 'input',
}
type ButtonProps = {
tag: Tags.button;
} & ({ a?: string; b?: undefined } | { a?: undefined; b?: string }) &
JSX.IntrinsicElements['button'];
type AnchorProps = {
tag: Tags.a;
} & ({ a?: string; b?: undefined } | { a?: undefined; b?: string }) &
JSX.IntrinsicElements['a'];
type InputProps = {
tag: Tags.input;
} & ({ a?: string; b?: undefined } | { a?: undefined; b?: …Run Code Online (Sandbox Code Playgroud) 我在bodyParser中遇到Express错误,无法解析任何PUT请求......我的配置设置如下:
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.query());
app.use(app.router);
Run Code Online (Sandbox Code Playgroud)
但是每次我向端点发出PUT请求时,req.body都会返回'undefined'.
我试过通过Chromes REST控制台发出请求,也通过jQuery ajax请求这样做:
$.ajax({
url: 'https://localhost:4430/api/locations/5095595b3d3b7b10e9f16cc1',
type: 'PUT',
data: {name: "Test name"},
dataType: 'json'
});
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我正在尝试计划我的api,例如:
/animals // returns all animals
/animals/dogs // returns all dogs
/animals/cats // returns all cats
/animals/dogs/:id // returns dog
Run Code Online (Sandbox Code Playgroud)
所以,我有'cat'和'dog'的单独模型,因为它们将包含独特的属性,但是它们都使用'animal'模式作为基础插件.
所以我的问题是关于存储数据的位置,我的选择是:
1)单个收集 - 将猫和狗存储在一个集合中,这将使得通过单个查询获得'/ animals'的数据相对容易
2)多个集合 - 将猫和狗存储在单独的集合中,这将使数据存储更加合理,但是当获取"/ animals"的数据时,将需要多个查询并连接该数据.
我还有其他任何选择,或者首选方法吗?
谢谢
angularjs ×3
mongoose ×3
node.js ×3
express ×2
mongodb ×2
angular ×1
forms ×1
javascript ×1
put ×1
reactjs ×1
rest ×1
schema ×1
setvalue ×1
typescript ×1
union-types ×1