码迷,mamicode.com
首页 > 其他好文 > 详细

mongoose 查询子文档的方法

时间:2015-12-04 18:01:06      阅读:396      评论:0      收藏:0      [点我收藏+]

标签:

 1 {
 2   "__v": 1,
 3   "_id": "538f5f0f6195a184108c8bd8",
 4   "title": "GameTitle",
 5   "item": [{
 6     "_id": "538f5f0f6195a184108c8bd6",
 7     "name": "itemOne",
 8     "men": [{
 9       "_id": "5390cccf0a84f41f37082874",
10       "user": "id22222222",
11       "score": 2000
12     }, {
13       "_id": "2390cccf0a84f41f37082873",
14       "user": "id33333333",
15       "score": 1000
16     }]
17   }, {
18     "_id": "538f5f0f6195a184108c8bd7",
19     "name": "itemTwo",
20     "men": []
21   }],
22   "status": 1
23 }
24  
25  
26 //代码是:
27     var MenSchema = new mongoose.Schema({
28     user: ‘String‘,
29     score: {
30         type: Number,
31         default: 0
32         }
33     });
34  
35     var ItemsSchema = new mongoose.Schema({
36         name: String 
37         ,men: [MenSchema]
38     });
39  
40     ListsSchema = new mongoose.Schema({
41         title: {
42             type: String,
43             required: true
44         }
45         ,item: [ItemsSchema]
46     });
47  
48     var Items = mongoose.model(‘item‘, ItemsSchema);
49     var Lists = mongoose.model(‘lists‘, ListsSchema);
50     var Men = mongoose.model(‘men‘, MenSchema);
51 Insert and update:
52  
53 function commit(sId, sItem, sUser, sIncreaseScore) {
54     Lists.findOne({, "_id": sId,
55             "item.name": sItem
56         }, null, function(err, documents) {
57             if (!err) {
58                 if (documents != null) {
59                     Lists.findOne({
60                         "_id": sId,
61                         "item.name": sItem,
62                         "item.men.user": sUser
63                     }, null, function(err, subDoc) {
64                         if (!err) {
65                             if (subDoc != null) {
66                                 //increase user score
67                                 //!!!!!!!!!!!!!But subDoc will get all arrays of item.men, so I can‘t update it correctly
68                             } else {
69                                 //inser new user score
70                                 var userData = new Men({
71                                     user: sUser,
72                                     score: sScore
73                                 });
74  
75                                 documents.item[0].men.push(userData);
76                                 documents.save(function(err) {
77                                     if (!err) {
78                                         ///!!!!!!!!!!!!!!Will come this
79                                         console.log("documents error on save!");
80                                     } else {
81                                         console.log("save documents ok!");
82                                     }
83                                 });
84                             }
85                         }
86                     });
87                 }
88             } else {
89                 console.log("not find the game item!");
90             }
91         }
92     );
93 }

这种查询方法比较特殊,直接用子文档的属性作为查询条件

 "item.men.user": sUser

也可以这样查找:

Lists.items.men.id("id");

此时查到的就是子文档中的某一条,而不是整个父文档,

执行删除

Lists.items.men.id("id");

在保存文档

mongoose 查询子文档的方法

标签:

原文地址:http://www.cnblogs.com/suoking/p/5019699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!