摘要:使用可以實現在一個中填充其他的。表示關聯注意被關聯的的必須是和才有效。封裝了很多查詢的方法,使得對數據庫的操作變得簡單啦。這里分享一下方法用法。類型的時,格式如為表示不填充,為時表示填充。類型,可選,指定附加的查詢條件。
Mongoose 是 MongoDB 的 ODM(Object Document Mapper)。
什么是ODM? 其實和ORM(Object Relational Mapper)是同類型的工具。都是將數據庫的數據轉化為代碼對象的庫,使用轉化后的對象可以直接對數據庫的數據進行CRUD(增刪改查)。
MongoDB 是文檔型數據庫(Document Database),不是關系型數據庫(RelationalDatabase)。而Mongoose可以將 MongonDB 數據庫存儲的文檔(documents)轉化為 javascript對象,然后可以直接進行數據的增刪改查。
因為MongoDB是文檔型數據庫,所以它沒有關系型數據庫joins(數據庫的兩張表通過"外鍵",建立連接關系。) 特性。也就是在建立數據的關聯時會比較麻煩。為了解決這個問題,Mongoose封裝了一個Population功能。使用Population可以實現在一個 document 中填充其他 collection(s) 的 document(s)。
在定義Schema的時候,如果設置某個 field 關聯另一個Schema,那么在獲取 document 的時候就可以使用 Population 功能通過關聯Schema的 field 找到關聯的另一個 document,并且用被關聯 document 的內容替換掉原來關聯字段(field)的內容。
接下來分享下:Query#populate Model#populate Document#populate的用法先建立三個Schema和Model:
var mongoose = require("mongoose"); var Schema = mongoose.Schema; var UserSchema = new Schema({ name : { type: String, unique: true }, posts : [{ type: Schema.Types.ObjectId, ref: "Post" }] }); var User = mongoose.model("User", UserSchema); var PostSchema = new Schema({ poster : { type: Schema.Types.ObjectId, ref: "User" }, comments : [{ type: Schema.Types.ObjectId, ref: "Comment" }], title : String, content : String }); var Post = mongoose.model("Post", PostSchema); var CommentSchema = new Schema({ post : { type: Schema.Types.ObjectId, ref: "Post" }, commenter : { type: Schema.Types.ObjectId, ref: "User" }, content : String }); var Comment = mongoose.model("Comment", CommentSchema);
在上述的例子中,創(chuàng)建了三個 Models:User,Post,Comment。
User 的屬性 post`,對應是一個 ObjectId 的數組。ref表示關聯Post(注意: 被關聯的model的 type 必須是ObjectId, Number, String, 和 Buffer 才有效)。
Post的屬性 poster 和 comments 分別關聯User和Comment。
Comment的屬性 post 和 commenter 分別關聯Post和User。
三個 Models 的關系:一個 user--has many-->post。一個 post--has one-->user,has many-->comment。一個 comment--has one-->post 和 user。
創(chuàng)建一些數據到數據庫:
// 連接數據庫 mongoose.connect("mongodb://localhost/population-test", function (err){ if (err) throw err; createData(); }); function createData() { var userIds = [new ObjectId, new ObjectId, new ObjectId]; var postIds = [new ObjectId, new ObjectId, new ObjectId]; var commentIds = [new ObjectId, new ObjectId, new ObjectId]; var users = []; var posts = []; var comments = []; users.push({ _id : userIds[0], name : "aikin", posts : [postIds[0]] }); users.push({ _id : userIds[1], name : "luna", posts : [postIds[1]] }); users.push({ _id : userIds[2], name : "luajin", posts : [postIds[2]] }); posts.push({ _id : postIds[0], title : "post-by-aikin", poster : userIds[0], comments : [commentIds[0]] }); posts.push({ _id : postIds[1], title : "post-by-luna", poster : userIds[1], comments : [commentIds[1]] }); posts.push({ _id : postIds[2], title : "post-by-luajin", poster : userIds[2], comments : [commentIds[2]] }); comments.push({ _id : commentIds[0], content : "comment-by-luna", commenter : userIds[1], post : postIds[0] }); comments.push({ _id : commentIds[1], content : "comment-by-luajin", commenter : userIds[2], post : postIds[1] }); comments.push({ _id : commentIds[2], content : "comment-by-aikin", commenter : userIds[1], post : postIds[2] }); User.create(users, function(err, docs) { Post.create(posts, function(err, docs) { Comment.create(comments, function(err, docs) { }); }); }); }
數據的準備就緒后,接下來就是探索populate方法:
Query#populate什么Query? Query(查詢),可以快速和簡單的從MongooDB查找出相應的 document(s)。 Mongoose封裝了很多查詢的方法,使得對數據庫的操作變得簡單啦。這里分享一下populate方法用法。
語法: Query.populate(path, [select], [model], [match], [options])
參數:
path
類型:String或Object。
String類型的時, 指定要填充的關聯字段,要填充多個關聯字段可以以空格分隔。
Object類型的時,就是把 populate 的參數封裝到一個對象里。當然也可以是個數組。下面的例子中將會實現。select
類型:Object或String,可選,指定填充document 中的哪些字段。
Object類型的時,格式如:{name: 1, _id: 0},為0表示不填充,為1時表示填充。
String類型的時,格式如:"name -_id",用空格分隔字段,在字段名前加上-表示不填充。詳細語法介紹query-selectmodel
類型:Model,可選,指定關聯字段的 model,如果沒有指定就會使用Schema的ref。
match
類型:Object,可選,指定附加的查詢條件。
options
類型:Object,可選,指定附加的其他查詢選項,如排序以及條數限制等等。
填充User的posts字段:
User.findOne({name: "aikin"}) .exec(function(err, doc) { var opts = [{ path : "posts", select : "title" }]; doc.populate(opts, function(err, populatedDoc) { console.log(populatedDoc.posts[0].title); // post-by-aikin }); });
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.hztianpu.com/yun/18935.html
摘要:使用可以實現在一個中填充其他的。表示關聯注意被關聯的的必須是和才有效。類型的時,格式如為表示不填充,為時表示填充。以鍵值對的形式表示?;卣{函數,接收兩個參數,錯誤和填充完的。參考數據庫的最簡單實現使用之原文鏈接 Mongoose 是 MongoDB 的 ODM(Object Document Mapper)。 什么是ODM? 其實和ORM(Object Relational Mapp...
摘要:是在環(huán)境下對進行便捷操作的對象模型工具因此,要使用,則必須安裝環(huán)境以及數據庫。使操作更簡單便捷。找到記錄,并且將遞增,返回后的為之前的。這個屬性很有用,對數字直接進行增減。,要返回的字段與的第二個參數一致。 Mongoose是在node.js環(huán)境下對mongodb進行便捷操作的對象模型工具 因此,要使用mongoose,則必須安裝node.js環(huán)境以及mongodb數據庫。mongoo...
摘要:在實際開發(fā)過程中發(fā)現,考試系統(tǒng)各個表集合都是需要關聯,這種非關系型數據庫,做起來反而麻煩了不少。數據中既有試卷的信息,也有很多題目。題目都屬于該試卷,改試卷又屬于當前登錄系統(tǒng)的老師即創(chuàng)建試卷的老師。 這是我畢業(yè)項目,從0到1,前后臺獨立開發(fā)完成。功能不多,在此記錄,溫故而知新!項目github地址:https://github.com/FinGet/Exam ,博客地址:https:/...
摘要:七牛云接入本系統(tǒng)的圖片,音視頻是放在七牛云,所以需要接入七牛云。在服務端通過接口請求來獲取七牛云上傳,客戶端獲取到七牛云,通過不同方案將帶上。 效果展示 showImg(https://user-gold-cdn.xitu.io/2018/8/26/16576a709bd02f5f?w=1409&h=521&f=gif&s=30128195); showImg(https://user...
摘要:七牛云接入本系統(tǒng)的圖片,音視頻是放在七牛云,所以需要接入七牛云。在服務端通過接口請求來獲取七牛云上傳,客戶端獲取到七牛云,通過不同方案將帶上。 效果展示 showImg(https://user-gold-cdn.xitu.io/2018/8/26/16576a709bd02f5f?w=1409&h=521&f=gif&s=30128195); showImg(https://user...
閱讀 3255·2021-09-10 10:51
閱讀 3448·2021-08-31 09:38
閱讀 1764·2019-08-30 15:54
閱讀 3194·2019-08-29 17:22
閱讀 3283·2019-08-26 13:53
閱讀 2036·2019-08-26 11:59
閱讀 3343·2019-08-26 11:37
閱讀 3373·2019-08-26 10:47