当前位置: 首页 > news >正文

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) }

http://www.lryc.cn/news/130354.html

相关文章:

  • Http 状态码汇总
  • mysql自定义实体类框架
  • 批量将Excel中的第二列内容从拼音转换为汉字
  • 消息推送:精准推送,提升运营效果,增添平台活力
  • [保研/考研机试] KY43 全排列 北京大学复试上机题 C++实现
  • Java将时间戳转化为特定时区的日期字符串
  • 【算法挨揍日记】day03——双指针算法_有效三角形的个数、和为s的两个数字
  • 通过 kk 创建 k8s 集群和 kubesphere
  • 感觉和身边其他人有差距怎么办?
  • 【C语言基础】宏定义的用法详解
  • 微服务系列文章之 SpringBoot 最佳实践
  • C++并发多线程--std::async、std::packaged_task和std::promise的使用
  • opencv-目标追踪
  • 【数据结构】 单链表面试题讲解
  • C++ string类的模拟实现
  • Qt实现简单的漫游器
  • 【c语言】文件操作
  • 【Unity】坐标转换经纬度方法(应用篇)
  • element时间选择器el-date-picter使用disabledDate指定禁用的日期
  • 出学校干了 5 年外包,已经废了
  • day-23 代码随想录算法训练营(19)part09
  • JVM编译优化
  • vue浏览器插件安装-各种问题
  • maven工具-maven的使用-镜像仓库、本地仓、IDEA使用maven
  • Mac鼠标增强工具Smooze Pro
  • 数据结构-单链表(C语言简单实现)
  • .netcore grpc身份验证和授权
  • 分布式 - 服务器Nginx:一小时入门系列之负载均衡
  • Linux学习之基本指令二
  • 神经网络基础-神经网络补充概念-41-梯度的数值逼近