小编Joh*_*yHK的帖子

如何检查Mongodb native nodejs驱动程序中是否存在集合?

我需要检查某个数据库中是否存在集合,如果不存在则创建它.我知道

db.createCollection(collName, {strict:true}, function(error, collection))
Run Code Online (Sandbox Code Playgroud)

collName在创建之前检查是否存在集合并设置error对象.但我需要一个独立的功能来检查.

mongodb node.js node-mongodb-native

36
推荐指数
6
解决办法
6万
查看次数

Azure Blob存储:DownloadToByteArray VS DownloadToStream

我一直在使用Azure Blob存储服务来保存/恢复要在Azure网页中托管的网页上下文中的文件.

在学习过程中,我提出了两个解决方案; 第一个基本上使用DownloadToStream哪个相同,但有一个FileStream.在这种情况下,我必须在将文件返回给用户之前将其写入服务器.

public static Stream GetFileContent(string fileName, HttpContextBase context)
{
      CloudBlobContainer container = GetBlobContainer();    
      CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);                                       
      Stream fileStream = new FileStream(
          context.Server.MapPath("~/App_Data/files/" + fileName), FileMode.Create);   
      blockBlob.DownloadToStream(fileStream);
      fileStream.Close();    
      return File.OpenRead(context.Server.MapPath("~/App_Data/files/" + fileName));
}

public ActionResult Download(string fileName)
{
    byte[] fileContent = MyFileContext.GetFileContent(fileName);
    return File(fileContent, "application/zip", fileName);        
}
Run Code Online (Sandbox Code Playgroud)

另一方面,我使用DownloadToByteArray函数将Blob的内容写入用Blob文件的大小初始化的字节数组中.

public static byte[] GetFileContent(string fileName)
{
    CloudBlobContainer container = GetBlobContainer();           
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
    blockBlob.FetchAttributes();
    long fileByteLength = blockBlob.Properties.Length;
    byte[] fileContent = new …
Run Code Online (Sandbox Code Playgroud)

c# azure

35
推荐指数
3
解决办法
3万
查看次数

在MongoDB中,如何索引数组中的嵌入对象字段?

多键的mongodb文档提供了查询数组中嵌入对象字段的示例:

http://www.mongodb.org/display/DOCS/Multikeys

但是没有解释你如何为这种情况创建索引.在数组上创建索引似乎不起作用(使用解释机制,您可以看到索引不可用).

额外细节:

> // find posts where julie commented
> db.posts.find( { "comments.author" : "julie" } )
{"title" : "How the west was won", 
 "comments" : [{"text" : "great!" , "author" : "sam"},
               {"text" : "ok" , "author" : "julie"}],
 "_id" : "497ce79f1ca9ca6d3efca325"}
Run Code Online (Sandbox Code Playgroud)

如果这样做db.articles.ensureIndex( { comments : 1 } ),则不会索引注释对象的子字段,而只会注释注释对象本身.

所以以下将使用索引:

 > db.posts.find( {comments : { "author" : "julie", "text" : "ok" } } )
Run Code Online (Sandbox Code Playgroud)

因为它在评论对象上搜索

但以下不会使用索引:

 > db.posts.find( { "comments.author" : "julie" } )
Run Code Online (Sandbox Code Playgroud)

那么如何让mongodb为第二种情况编制索引呢?

mongodb

28
推荐指数
2
解决办法
2万
查看次数

Mongoose创建多个文档

我知道在最新版本的Mongoose中你可以将多个文件传递给create方法,或者在我的情况下甚至可以更好地传递一组文档.

var array = [{ type: 'jelly bean' }, { type: 'snickers' }];
Candy.create(array, function (err, jellybean, snickers) {
    if (err) // ...
});
Run Code Online (Sandbox Code Playgroud)

我的问题是数组的大小是动态的,所以在回调中有一个创建对象的数组会很有帮助.

var array = [{ type: 'jelly bean' }, { type: 'snickers' }, ..... {type: 'N candie'}];
Candy.create(array, function (err, candies) {
    if (err) // ...

    candies.forEach(function(candy) {
       // do some stuff with candies
    });
});
Run Code Online (Sandbox Code Playgroud)

不在文档中,但是这样可能吗?

mongoose node.js

28
推荐指数
4
解决办法
3万
查看次数

没有nodejs的Nodejs库

如何将nodejs库集成到我的非nodejs项目中?我特别需要这个库:https: //github.com/greenify/biojs-io-blast

javascript node.js

27
推荐指数
1
解决办法
2158
查看次数

如何在MongoDB中应用约束?

我已经开始使用MongoDB了,我对它很新.有什么办法可以在MongoDB中对文档应用约束吗?像指定主键或将属性视为唯一?或者指定特定属性是否大于最小值?

mongodb

22
推荐指数
2
解决办法
3万
查看次数

更新时出现Sql错误:UPDATE语句与FOREIGN KEY约束冲突

我有一个名为table的表patient_address,它在patient表中引用了一个PK键.但是,如果我尝试运行以下语句之一:

update patient set id_no='7008255601088' where id_no='8008255601088'
update patient_address set id_no='7008255601088' where id_no='8008255601088'
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

"UPDATE语句与REFERENCE约束冲突"FK__patient_a__id_no__27C3E46E".冲突发生在数据库"PMS",表"dbo.patient_address",列'id_no'." 或"UPDATE语句与FOREIGN KEY约束冲突"FK__patient_a__id_no__27C3E46E".冲突发生在数据库"PMS",表"dbo.patient",列'id_no'." .

有没有人知道可能的原因?谢谢.

sql-server

22
推荐指数
3
解决办法
7万
查看次数

Mongodb:从mongo shell中的ObjectId执行日期范围查询

我有一个看起来像这样的集合:

{
  _id: ObjectId("50a68673476427844b000001"),
  other fields
}
Run Code Online (Sandbox Code Playgroud)

我想做一个范围查询来查找两个日期之间的记录.我知道我可以从mongo shell var中的ObjectId获取日期:

var aDate = ObjectId().getTimestamp()
Run Code Online (Sandbox Code Playgroud)

但是目前还没有一种方法(据我现在可以搞清楚)创建一个仅包含时间戳部分的ObjectId - 我认为我的理想解决方案是无功能的mongo shell代码将是:

var minDate = ObjectId(new Date("2012-11-10"));
var maxDate = ObjectId(new Date("2012-11-17"));
Run Code Online (Sandbox Code Playgroud)

使用minDate和MaxDate作为范围值.

有没有办法在SHELL中做到这一点 - 我对一些驱动程序产品不感兴趣.

mongodb mongodb-shell

21
推荐指数
2
解决办法
1万
查看次数

Mongoose填充子子文档

我在MongoDB中有这个设置

项目:

title: String
comments: [] // of objectId's
Run Code Online (Sandbox Code Playgroud)

评论:

user: ObjectId()
item: ObjectId()
comment: String
Run Code Online (Sandbox Code Playgroud)

这是我的Mongoose架构:

itemSchema = mongoose.Schema({
    title: String,
    comments: [{ type: Schema.Types.ObjectId, ref: 'comments' }],
});

Item = mongoose.model('items', itemSchema);

commentSchema = mongoose.Schema({
    comment: String,
    user: { type: Schema.Types.ObjectId, ref: 'users' },
});

Comment = mongoose.model('comments', commentSchema);
Run Code Online (Sandbox Code Playgroud)

这是我得到我的项目和评论的地方:

Item.find({}).populate('comments').exec(function(err, data){
    if (err) return handleError(err);
    res.json(data);
});
Run Code Online (Sandbox Code Playgroud)

如何使用相应的用户填充注释数组?由于每条评论都有一个用户ObjectId()?

javascript mongoose mongodb node.js

20
推荐指数
6
解决办法
2万
查看次数

Mongoose总是返回一个空数组NodeJS

我尝试过使用find并且findOne两者都没有返回文档.find是在返回一个空数组findOne被返回null.err在这两种情况下null也是如此.

这是我的联系:

function connectToDB(){
    mongoose.connect("mongodb://localhost/test"); //i have also tried 127.0.0.1
    db = mongoose.connection;
    db.on("error", console.error.bind(console, "connection error:"));
    db.once("open", function callback(){
        console.log("CONNECTED");
    });
};
Run Code Online (Sandbox Code Playgroud)

这是我的架构:

var fileSchema = mongoose.Schema({
    hash: String,
    type: String,
    extension: String,
    size: String,
    uploaded: {type:Date, default:(Date.now)},
    expires: {type:Date, default:(Date.now()+oneDay)}
});
var Model = mongoose.model("Model", fileSchema);
Run Code Online (Sandbox Code Playgroud)

我的查询在这里:

Model.find({},function(err, file) {
    console.log(err)
    console.log(file);  
});
Run Code Online (Sandbox Code Playgroud)

我可以将内容上传到数据库并通过RockMongo查看,但我无法在之后获取它们.这是我第一次使用MongoDB,所以我想我只是缺少一些基础知识.任何正确方向的推动都会很棒!

mongoose mongodb node.js

18
推荐指数
1
解决办法
1万
查看次数