我正在尝试使用mongoose返回dbs集合的列表.我按照此处列出的指示,但http://grokbase.com/t/gg/mongoose-orm/122xxxr7qy/mongoose-get-a-list-of-all-collections.所以这是我的代码
var mongoose = require('mongoose');
//if (mongoose.connection.readyState == 0){//checks if already connected to the database
console.log("creating connection to the database");
var Config = require('../configs/config');
var config = new Config();
config = config.getConfig().db.dev;
if (mongoose.connection.readyState = 0 ) {
mongoose.connect("mongodb://austin:password1@paulo.mongohq.com:10023/test1");
console.log('mongoose readyState is ' + mongoose.connection.readyState);
}
var collection;
mongoose.connection.on('open', function (ref) {
console.log('Connected to mongo server.');
});
//trying to get collection names
mongoose.connection.db.collectionNames(function (err, names) {
console.log(names); // [{ name: 'dbname.myCollection' }]
module.exports.Collection = names;
});
Run Code Online (Sandbox Code Playgroud)
唯一的问题是名称返回未定义.那么甚至可以使用香草猫鼬返回集合列表吗?
我正在尝试编写一个测试,如果输入是字符串或空值,它将通过
在柴是有类似的东西
expect(foo).to.be.a('string').or.a('null')
Run Code Online (Sandbox Code Playgroud)
如果不是,在编写需要检查多种类型的测试时最佳做法是什么?
所以我有两个模式
var subcategories = new Schema({
//the category being populated needs to be the same case ;
categoryId: [{ type: Schema.ObjectId, ref: 'categories' }],
name: String,
description: String,
display: Boolean,
active: Boolean,
sortOrder: Number,
createDate: Date,
updateDate: Date,
type: String,
startDate: Date,
endDate: Date,
authorId: String
});
Run Code Online (Sandbox Code Playgroud)
和
var categories = new Schema({
name: String,
description: String,
display: Boolean,
active: Boolean,
sortOrder: Number,
createDate: Number,
updateDate: Number,
type: String,
startDate: Date,
endDate: Date,
authorId: String
});
Run Code Online (Sandbox Code Playgroud)
并且我希望只有在类别/子类别中的active/display为true时才返回查询.我遇到的问题是如何在填充后正确设置categoryId的过滤器.这是我到目前为止所拥有的
exports.generateList = function …Run Code Online (Sandbox Code Playgroud) 请考虑以下脚本(用于local_var2的算术语法与此情况无关):
#!/bin/ksh
function my_func1
{
typeset local_var1=one
typeset local_var2
(( local_var2 = 1 ))
echo my_func1: local_var1 = $local_var1, local_var2 = $local_var2
}
my_func2()
{
typeset local_var1=two
typeset local_var2
(( local_var2 = 2 ))
echo my_func2: local_var1 = $local_var1, local_var2 = $local_var2
}
local_var1=0
local_var2=0
echo before functions: local_var1 = $local_var1, local_var2 = $local_var2
my_func1
echo after my_func1: local_var1 = $local_var1, local_var2 = $local_var2
my_func2
echo after my_func2: local_var1 = $local_var1, local_var2 = $local_var2
Run Code Online (Sandbox Code Playgroud)
运行时,它会产生以下输出:
before functions: local_var1 = 0, …Run Code Online (Sandbox Code Playgroud) 我正在尝试为一个类创建一个简单的C程序,其中一个要求是我需要使用scanf/ printf用于所有输入和输出.我的问题是为什么我scanf跳过主循环中的for循环并且程序刚终止.
这是我的代码
#include <stdio.h>
void main() {
int userValue;
int x;
char c;
printf("Enter a number : ");
scanf("%d", &userValue);
printf("The odd prime values are:\n");
for (x = 3; x <= userValue; x = x + 2) {
int a;
a = isPrime(x);
if (a = 1) {
printf("%d is an odd prime\n", x);
}
}
printf("hit anything to terminate...");
scanf("%c", &c);
}
int isPrime(int number) {
int i;
for (i = 2; i …Run Code Online (Sandbox Code Playgroud) 假设我有一个带有嵌入式文档的模式,称为包含多个值的首选项.我试图让mongoose默认嵌入文档为null
例如,我在过去的基本类型(如字符串和日期)中将字段默认为null.
var userSchema = new Schema({
name: {type: String, required: true},
preferences: {type: preferences, default: null}
});
var preferences: {
preference1: String,
preference2: String
}
Run Code Online (Sandbox Code Playgroud)
在新文档的创建中,我应该如何将userSchema中的属性首选项默认为null?
我正在阅读essisentals第8版的操作系统概念.当作者查看连续的内存分配和最差拟合时,作者说"分配最大的洞.再次,我们必须搜索整个列表,除非它按大小排序.这个策略产生最大的剩余空洞,这可能比从最合适的方法中获得较小的剩余孔."
所以我的问题是,什么时候离开最大的剩余洞更好的方法?
让我先说明我知道mongoose提供的填充方法,但由于我的工作决定将来转移到本地mongodb驱动程序,我不能再依赖填充来避免为自己工作了.
如果我有两个文档集合
People
{_id:1, name:Austin}
{_id:2, name:Doug}
{_id:3, name:Nick}
{_id:4, name:Austin}
Hobbies:
{Person: 1, Hobby: Cars}
{Person:1, Hobby: Boats}
{Person:3, Hobby: Chess}
{Person:4, Hobby: Cars}
Run Code Online (Sandbox Code Playgroud)
我应该如何加入爱好者的每个文件.理想情况下,我宁愿只需要调用数据库两次以获取人员,第二次调用业余爱好,然后返回客户端应用程序对象,并将它们加入到eeter中.
这是我试图转换为查询字符串的javascript对象
{$and: [{topic: categoryIds} , {$or :[ {'groups 1': {$ne: ''}}, {groups: $scope.myGroups}]}]};
Run Code Online (Sandbox Code Playgroud)
基本上我希望匹配一个等于categoryIds的主题,并抓取具有空组数组的文档,或者groups数组具有值并匹配数组$ scope.mygroups中的一个
我的问题是,以易于解析的格式转换它的最佳做法是什么,以便我可以将它附加到GET请求,以及如何在快速服务器上解析它.
我正在尝试从子文档数组中获取特定字段
我不会在父文档中包含任何字段
这是示例文档
{
"_id" : ObjectId("5409dd36b71997726532012d"),
"hierarchies" : [
{
"rank" : 1,
"_id" : ObjectId("5409df85b719977265320137"),
"name" : "CTO",
"userId" : [
ObjectId("53a47a639c52c9d83a2d71db")
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
如果userId在userId数组中,我想返回层次结构的等级
这是我在查询中到目前为止的内容
collectionName.find({{hierarchies:
{$elemMatch : {userId: ObjectId("53a47a639c52c9d83a2d71db")}}}
, "hierarchies.$.rank", function(err,data){}
Run Code Online (Sandbox Code Playgroud)
到目前为止,它返回我想要的层次结构数组中的整个对象,但我想将其限制为对象的rank属性.
mongoose ×6
mongodb ×5
node.js ×3
angularjs ×1
arrays ×1
bash ×1
c ×1
chai ×1
contiguous ×1
express ×1
javascript ×1
ksh ×1
query-string ×1
scanf ×1
schema ×1
subdocument ×1
unit-testing ×1