mongoose 查询子文档的方法
{
"__v": 1,
"_id": "538f5f0f6195a184108c8bd8",
"title": "GameTitle",
"item": [{
"_id": "538f5f0f6195a184108c8bd6",
"name": "itemOne",
"men": [{
"_id": "5390cccf0a84f41f37082874",
"user": "id22222222",
"score": 2000
}, {
"_id": "2390cccf0a84f41f37082873",
"user": "id33333333",
"score": 1000
}]
}, {
"_id": "538f5f0f6195a184108c8bd7",
"name": "itemTwo",
"men": []
}],
"status": 1
}
//代码是:
var MenSchema = new mongoose.Schema({
user: 'String',
score: {
type: Number,
default: 0
}
});
var ItemsSchema = new mongoose.Schema({
name: String
,men: [MenSchema]
});
ListsSchema = new mongoose.Schema({
title: {
type: String,
required: true
}
,item: [ItemsSchema]
});
var Items = mongoose.model('item', ItemsSchema);
var Lists = mongoose.model('lists', ListsSchema);
var Men = mongoose.model('men', MenSchema);
Insert and update:
function commit(sId, sItem, sUser, sIncreaseScore) {
Lists.findOne({, "_id": sId,
"item.name": sItem
}, null, function(err, documents) {
if (!err) {
if (documents != null) {
Lists.findOne({
"_id": sId,
"item.name": sItem,
"item.men.user": sUser
}, null, function(err, subDoc) {
if (!err) {
if (subDoc != null) {
//increase user score
//!!!!!!!!!!!!!But subDoc will get all arrays of item.men, so I can't update it correctly
} else {
//inser new user score
var userData = new Men({
user: sUser,
score: sScore
});
documents.item[0].men.push(userData);
documents.save(function(err) {
if (!err) {
///!!!!!!!!!!!!!!Will come this
console.log("documents error on save!");
} else {
console.log("save documents ok!");
}
});
}
}
});
}
} else {
console.log("not find the game item!");
}
}
);
}
这种查询方法比较特殊,直接用子文档的属性作为查询条件
"item.men.user": sUser 也可以这样查找:
Lists.items.men.id("id");
此时查到的就是子文档中的某一条,而不是整个父文档,
执行删除
Lists.items.men.id("id");
在保存文档
mongoose 查询子文档的方法的更多相关文章
- mongoose子文档生成不了_id,查询困难的解决办法
以下是我的数据库表 本来是想设置一个自增属性,比如listId,来定义我的子list,sliderlist这些,这样能确保他的唯一性,结果尝试了很久都无法成功,原生,插件都无法成功, 这两个网址是讲的 ...
- ES 07 - Elasticsearch查询文档的六种方法
目录 1 Query String Search(查询串检索) 2 Query DSL(ES特定语法检索) 3 Query Filter(过滤检索) 4 Full Text Search(全文检索) ...
- mongodb子文档查询
--子文档分页 -- 测试数据 db.childTests.insert({ "_id" : 1, "item" : "ABC", &quo ...
- mongoDB多级子文档查询
db.getCollection('product').find({'coverage':{'$elemMatch':{'plan':{'$elemMatch':{'iscoverage':{'$in ...
- mongoose查询数据库步骤
建立dumall数据库,创建goods集合,导入数据文件,也可以自己手动插入. mongodb安装与环境搭建: http://www.cnblogs.com/ccyinghua/p/7887713.h ...
- ASP.NET MVC 5 - 查询Details和Delete方法
在这部分教程中,接下来我们将讨论自动生成的Details和Delete方法. 查询Details和Delete方法 打开Movie控制器并查看Details方法. public ActionResul ...
- Solr嵌套子文档的弊端以及一种替代方式
背景:在考察了多种工具后,我们决定使用solr来作为多标签用户管理体系的查询方案. 原计划:电话,call客,跟进等等记录上报到kafka,然后通过flume+morphline录入到solr中.每一 ...
- iStylePDF安全电子文档解决方案之电子合同在线订立
交易是商业世界不可或缺的一部分,而签名是交易的凭证.可是,尽管互联网和IT技术已经很发达,但每逢遇到签名,还是得用最原始的方法——握笔写字.与如今走到哪都能听到“互联网+”相比有点不合潮流,通过电子签 ...
- mongodb查询文档
说到查询,我们一般就想起了关系型数据库的查询了,比如:order by(排序).limit(分页).范围查询(大于某个值,小于某个值..,in查询,on查询,like查询等待很多),同样mongodb ...
随机推荐
- 【git】error: Your local changes to the following files
今天在服务器上git pull是出现以下错误: error: Your local changes to the following files would be overwritten by mer ...
- 关于“创业者与VC见面的10个不成文细节点”
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:Will Wang链接:http://www.zhihu.com/question/19641135/answer/50974 ...
- 微信的分享功能(针对web手机站页面进行的分享功能)
把这段js粘贴进,设置可以分享的页面,当微信打开,即可微信进行分享各个圈 $(function(){ var lujing=$("#logimg").attr("src& ...
- poj3077---进位
#include <stdio.h> #include <stdlib.h> #include<string.h> ]; ]; int main() { int n ...
- C#获取单个字符的拼音声母
public class ConvertToPinYing { /// <summary> /// 汉字转拼音缩写 /// < ...
- MVC中使用AuthorizeAttribute做身份验证操作【转】
http://blog.csdn.net/try530/article/details/7782704 代码顺序为:OnAuthorization-->AuthorizeCore-->Ha ...
- dev gridcontrol 单箱效果
private void gridView1_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChan ...
- POJ2528线段树基础
開始就直接用延迟标记搞了下.最后发现内存肯定会爆了.数据太大了. 问了瓜神,原来应该用离散化来做这题,详细见凝视 #include <cstdio> #include <cstrin ...
- CLR via C# 阅读笔记
1.char在C#中为16位Unicode字符:int 映射到System.Int32;long映射到System.Int64. 2.重载时C#不考虑返回值,而CLR允许返回值不同,方法名和参数相同的 ...
- NotificationManager 发送通知
该应用的界面如下,界面代码在此不再给出,源码github账户下载 MainActivity.java public class MainActivity extends Activity { priv ...