MongoDB:如何知道MongoDB是否包含特定文档

时间:2015-07-10 23:15:47

标签: mongodb meteor

例如我有集合(Posts = new Mongo.Collection('posts'); Posts.insert({ name: "John", ....etc. )}; var TMP = { name: "John", ....etc. }; ),我想知道,如果我的帖子包含一些文档,例如:

>> M = [A;B;C]; % concatenate into a single matrix
>> [~,idx] = max(M); % get index of maximum in each column
>> vals = hist(idx, 1:size(M,1)) % make histogram of results

vals =

     1     1     2

我想知道我的帖子是否有一些“等于”TMP的文件

我怎么做 ???

1 个答案:

答案 0 :(得分:0)

这将检查包含TMP对象中所有键/值的文档。如果没有匹配的文档,它将返回undefined。

我将以下代码放在某个服务器文件夹中:

Posts = new Mongo.Collection("posts");

var postExists;

var TMP = {
  name : "John",
  age: 26
};

// postExists should return undefined when TMP is not in the collection
//  !!postExists will evaluate to false
postExists = Posts.findOne(TMP);
console.log("When doc doesn't exists, postExists = " + postExists);

Posts.insert(TMP);

// postExists will return the object when if it does exists
// !!postExists will evaluate to truew
postExists = Posts.findOne(TMP);
console.log("When doc does exist, postExists = " + postExists);
相关问题