How to deal with document-oriented data

  • Schema design
  • Data models for e-commerce
  • Nuts and bolts of databases, collection, and documents.

Principles of schema design

  • What are your application access pattern?
  • What's the basic unit of data? the basic unit of data is the BSON document
  • What are the capabilities of your database?
  • What makes a good unique id or primary key for a record?

Designing an e-commerce data model

[root@DBAMAXWELL ~]# vi user_actions.rb
require 'mongo'
VIEW_PRODUCT = 0 # action type constants

ADD_TO_CART = 1
CHECKOUT = 2
PURCHASE = 3
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'garden')
client[:user_actions].drop
actions = client[:user_actions, :capped => true, :size => 16384]

actions.create
500.times do |n| # loop 500 times, using n as the iterator
 doc = {
 :username => "kbanker",
 :action_code => rand(4), # random value between 0 and 3, inclusive
 :time => Time.now.utc,
 :n => n
 }
 actions.insert_one(doc)
end

~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
~                                                                                                                                                 
"user_actions.rb" [New] 18L, 508C written
[root@DBAMAXWELL ~]# chmod 775 user_actions.rb
[root@DBAMAXWELL ~]# ruby user_actions.rb
[root@DBAMAXWELL ~]# mongo
MongoDB shell version v4.4.13
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b9c56822-9b9a-4d7b-8a11-1e9b31be1f7b") }
MongoDB server version: 4.4.13
---
The server generated these startup warnings when booting: 
        2022-03-23T23:33:56.417+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> use garden
switched to db garden
> db.user_actions.count()
199
> db.user_actions.find().pretty()
{
        "_id" : ObjectId("623eaf017c6c2558c3a33434"),
        "username" : "kbanker",
        "action_code" : 0,
        "time" : ISODate("2022-03-26T06:13:21.193Z"),
        "n" : 301
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a33435"),
        "username" : "kbanker",
        "action_code" : 2,
        "time" : ISODate("2022-03-26T06:13:21.195Z"),
        "n" : 302
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a33436"),
        "username" : "kbanker",
        "action_code" : 3,
        "time" : ISODate("2022-03-26T06:13:21.196Z"),
        "n" : 303
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a33437"),
        "username" : "kbanker",
        "action_code" : 3,
        "time" : ISODate("2022-03-26T06:13:21.197Z"),
        "n" : 304
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a33438"),
        "username" : "kbanker",
        "action_code" : 0,
        "time" : ISODate("2022-03-26T06:13:21.199Z"),
        "n" : 305
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a33439"),
        "username" : "kbanker",
        "action_code" : 3,
        "time" : ISODate("2022-03-26T06:13:21.201Z"),
        "n" : 306
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a3343a"),
        "username" : "kbanker",
        "action_code" : 3,
        "time" : ISODate("2022-03-26T06:13:21.203Z"),
        "n" : 307
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a3343b"),
        "username" : "kbanker",
        "action_code" : 1,
        "time" : ISODate("2022-03-26T06:13:21.204Z"),
        "n" : 308
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a3343c"),
        "username" : "kbanker",
        "action_code" : 0,
        "time" : ISODate("2022-03-26T06:13:21.206Z"),
        "n" : 309
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a3343d"),
        "username" : "kbanker",
        "action_code" : 1,
        "time" : ISODate("2022-03-26T06:13:21.208Z"),
        "n" : 310
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a3343e"),
        "username" : "kbanker",
        "action_code" : 1,
        "time" : ISODate("2022-03-26T06:13:21.210Z"),
        "n" : 311
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a3343f"),
        "username" : "kbanker",
        "action_code" : 1,
        "time" : ISODate("2022-03-26T06:13:21.212Z"),
        "n" : 312
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a33440"),
        "username" : "kbanker",
        "action_code" : 3,
        "time" : ISODate("2022-03-26T06:13:21.214Z"),
        "n" : 313
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a33441"),
        "username" : "kbanker",
        "action_code" : 3,
        "time" : ISODate("2022-03-26T06:13:21.216Z"),
        "n" : 314
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a33442"),
        "username" : "kbanker",
        "action_code" : 1,
        "time" : ISODate("2022-03-26T06:13:21.217Z"),
        "n" : 315
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a33443"),
        "username" : "kbanker",
        "action_code" : 1,
        "time" : ISODate("2022-03-26T06:13:21.219Z"),
        "n" : 316
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a33444"),
        "username" : "kbanker",
        "action_code" : 1,
        "time" : ISODate("2022-03-26T06:13:21.221Z"),
        "n" : 317
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a33445"),
        "username" : "kbanker",
        "action_code" : 2,
        "time" : ISODate("2022-03-26T06:13:21.223Z"),
        "n" : 318
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a33446"),
        "username" : "kbanker",
        "action_code" : 0,
        "time" : ISODate("2022-03-26T06:13:21.225Z"),
        "n" : 319
}
{
        "_id" : ObjectId("623eaf017c6c2558c3a33447"),
        "username" : "kbanker",
        "action_code" : 0,
        "time" : ISODate("2022-03-26T06:13:21.227Z"),
        "n" : 320
}
Type "it" for more
> db.createCollection("users.actions",{capped: true, size: 16384, max: 100})
{ "ok" : 1 }
> db.reviews.createIndex({time_field: 1}, {expireAfterSeconds: 3600})
{
        "createdCollectionAutomatically" : true,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.system.namespaces.find()
> db.system.namespaces.find();
> db.system.namespaces.find();
> db.system.indexes.find();

Documents and insertion

> db.numbers.save({n: 5})
WriteResult({ "nInserted" : 1 })
> db.numbers.save({n: NumberLong(5)});
WriteResult({ "nInserted" : 1 })
> db.numbers.find({n: 5});
{ "_id" : ObjectId("623ec3abe05ac712a59cc348"), "n" : 5 }
{ "_id" : ObjectId("623ec3cde05ac712a59cc349"), "n" : NumberLong(5) }
> db.numbers.find({n: {$type: 1}});
{ "_id" : ObjectId("623ec3abe05ac712a59cc348"), "n" : 5 }
> db.numbers.find({n: {$type: 18}});
{ "_id" : ObjectId("623ec3cde05ac712a59cc349"), "n" : NumberLong(5) }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值