当前位置: 首页 > 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) }

相关文章:

How to deal with document-oriented data

Schema designData models for e-commerceNuts and bolts of databases, collection, and documents. Principles of schema design What are your application access pattern?Whats the basic unit of data? the basic unit of data is the BSON documentWhat are the ca…...

Http 状态码汇总

文章目录 Http 状态码汇总1xx(信息性状态码)2xx(成功状态码)3xx(重定向状态码)4xx(客户端错误状态码)5xx(服务器错误状态码) Http 状态码汇总 1xx&#xff08…...

mysql自定义实体类框架

根据表结构自动生产实体类和方法,根据反射与io生成,可自定义扩展方法 package com.digital.web.front; /*** pom依赖* <dependency>* <groupId>mysql</groupId>* <artifactId>mysql-connector-java</artifactId>* <version>5.1.27</ve…...

批量将Excel中的第二列内容从拼音转换为汉字

要批量将Excel中的第二列内容从拼音转换为汉字&#xff0c;您可以使用Python的openpyxl库来实现。下面是一个示例代码&#xff0c;演示如何读取Excel文件并将第二列内容进行拼音转汉字&#xff1a; from openpyxl import load_workbook from xpinyin import Pinyin # 打开Exce…...

消息推送:精准推送,提升运营效果,增添平台活力

对于app开发者而言&#xff0c;没有什么途径比消息推送更能直接、即时地触及目标用户群体了。消息推送与我们的日常生活息息相关&#xff0c;各种APP的状态和通知都通过消息推送来告知用户&#xff0c;引起用户的注意&#xff0c;吸引用户点开app。总而言之&#xff0c;推送服务…...

[保研/考研机试] KY43 全排列 北京大学复试上机题 C++实现

题目链接&#xff1a; 全排列https://www.nowcoder.com/share/jump/437195121692001512368 描述 给定一个由不同的小写字母组成的字符串&#xff0c;输出这个字符串的所有全排列。 我们假设对于小写字母有a < b < ... < y < z&#xff0c;而且给定的字符串中的字…...

Java将时间戳转化为特定时区的日期字符串

先上代码&#xff1a; ZonedDateTime dateTime ZonedDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()),zone ); //2019-12-01T19:01:4608:00String formattedDate dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd") ); //2019-12-…...

【算法挨揍日记】day03——双指针算法_有效三角形的个数、和为s的两个数字

611. 有效三角形的个数 611. 有效三角形的个数https://leetcode.cn/problems/valid-triangle-number/ 题目描述&#xff1a; 给定一个包含非负整数的数组 nums &#xff0c;返回其中可以组成三角形三条边的三元组个数。 解题思路&#xff1a; 本题是一个关于三角形是否能成立…...

通过 kk 创建 k8s 集群和 kubesphere

官方文档&#xff1a;多节点安装 确保从正确的区域下载 KubeKey export KKZONEcn下载 KubeKey curl -sfL https://get-kk.kubesphere.io | VERSIONv3.0.7 sh -为 kk 添加可执行权限&#xff1a; chmod x kk创建 config 文件 KubeSphere 版本&#xff1a;v3.3 支持的 Kuber…...

感觉和身边其他人有差距怎么办?

虽然清楚知识需要靠时间沉淀&#xff0c;但在看到自己做不出来的题别人会做&#xff0c;自己写不出的代码别人会写时还是会感到焦虑怎么办&#xff1f; 你是否也因为自身跟周围人的差距而产生过迷茫&#xff0c;这份迷茫如今是被你克服了还是仍旧让你感到困扰&#xff1f; 下…...

【C语言基础】宏定义的用法详解

&#x1f4e2;&#xff1a;如果你也对机器人、人工智能感兴趣&#xff0c;看来我们志同道合✨ &#x1f4e2;&#xff1a;不妨浏览一下我的博客主页【https://blog.csdn.net/weixin_51244852】 &#x1f4e2;&#xff1a;文章若有幸对你有帮助&#xff0c;可点赞 &#x1f44d;…...

微服务系列文章之 SpringBoot 最佳实践

Spring Boot 是一种广泛使用且非常流行的企业级高性能框架。 以下是一些最佳实践和一些技巧&#xff0c;我们可以使用它们来改进 Spring Boot 应用程序并使其更加高效。 Spring Boot 的四大核心 1、自动配置 针对很多Spring应用程序和常见的应用功能&#xff0c;Spring Boo…...

C++并发多线程--std::async、std::packaged_task和std::promise的使用

目录 1--std::async的使用 2--std::packaged_task的使用 3--std::promise的使用 1--std::async的使用 std::async用于启动一个异步任务&#xff0c;并返回一个std::future对象&#xff1b;std::future对象里含有异步任务线程入口函数的结果&#xff1b; std::launch::deferr…...

opencv-目标追踪

import argparse import time import cv2 import numpy as np# 配置参数 ap argparse.ArgumentParser() ap.add_argument("-v", "--video", typestr,help"path to input video file") ap.add_argument("-t", "--tracker", …...

【数据结构】 单链表面试题讲解

文章目录 引言反转单链表题目描述示例&#xff1a;题解思路代码实现&#xff1a; 移除链表元素题目描述&#xff1a;示例思路解析&#xff1a; 链表的中间结点题目描述&#xff1a;示例&#xff1a;思路解析代码实现如下&#xff1a; 链表中倒数第k个结点题目描述示例思路解析&…...

C++ string类的模拟实现

模拟实现string类不是为了造一个更好的轮子&#xff0c;而是更加理解string类&#xff0c;从而来掌握string类的使用 string类的接口设计繁多&#xff0c;故而不会全部涵盖到&#xff0c;但是核心的会模拟实现 库中string类是封装在std的命名空间中的&#xff0c;所以在模拟…...

Qt实现简单的漫游器

文章目录 Qt的OpenGL窗口GLSL的实现摄像机类的实现简单的漫游器 Qt的OpenGL窗口 Qt主要是使用QOpenGLWidget来实现opengl的功能。  QOpenGLWidget 提供了三个便捷的虚函数&#xff0c;可以重载&#xff0c;用来重新实现典型的OpenGL任务&#xff1a; paintGL&#xff1a;渲染…...

【c语言】文件操作

朋友们&#xff0c;大家好&#xff0c;今天分享给大家的是文件操作的相关知识&#xff0c;跟着我一起学习吧&#xff01;&#xff01; &#x1f388;什么是文件 磁盘上的文件是文件。 但是在程序设计中&#xff0c;我们一般谈的文件有两种&#xff1a;程序文件、数据文件 程序文…...

【Unity】坐标转换经纬度方法(应用篇)

【Unity】坐标转换经纬度方法&#xff08;应用篇&#xff09; 解决地图中经纬度坐标转换与unity坐标互转的问题。使用线性变换的方法&#xff0c;理论上可以解决小范围内所以坐标转换的问题。 之前有写过[Unity]坐标转换经纬度方法&#xff08;原理篇),在实际使用中&#xff0c…...

element时间选择器el-date-picter使用disabledDate指定禁用的日期

需要的效果 <el-date-pickerclass"selectstyle"v-model"year"value-format"yyyy"type"year":picker-options"disabledCli"placeholder"选择年"> </el-date-picker>data() {return {disabledCli: {/…...

React 第五十五节 Router 中 useAsyncError的使用详解

前言 useAsyncError 是 React Router v6.4 引入的一个钩子&#xff0c;用于处理异步操作&#xff08;如数据加载&#xff09;中的错误。下面我将详细解释其用途并提供代码示例。 一、useAsyncError 用途 处理异步错误&#xff1a;捕获在 loader 或 action 中发生的异步错误替…...

css实现圆环展示百分比,根据值动态展示所占比例

代码如下 <view class""><view class"circle-chart"><view v-if"!!num" class"pie-item" :style"{background: conic-gradient(var(--one-color) 0%,#E9E6F1 ${num}%),}"></view><view v-else …...

UE5 学习系列(三)创建和移动物体

这篇博客是该系列的第三篇&#xff0c;是在之前两篇博客的基础上展开&#xff0c;主要介绍如何在操作界面中创建和拖动物体&#xff0c;这篇博客跟随的视频链接如下&#xff1a; B 站视频&#xff1a;s03-创建和移动物体 如果你不打算开之前的博客并且对UE5 比较熟的话按照以…...

镜像里切换为普通用户

如果你登录远程虚拟机默认就是 root 用户&#xff0c;但你不希望用 root 权限运行 ns-3&#xff08;这是对的&#xff0c;ns3 工具会拒绝 root&#xff09;&#xff0c;你可以按以下方法创建一个 非 root 用户账号 并切换到它运行 ns-3。 一次性解决方案&#xff1a;创建非 roo…...

Mobile ALOHA全身模仿学习

一、题目 Mobile ALOHA&#xff1a;通过低成本全身远程操作学习双手移动操作 传统模仿学习&#xff08;Imitation Learning&#xff09;缺点&#xff1a;聚焦与桌面操作&#xff0c;缺乏通用任务所需的移动性和灵活性 本论文优点&#xff1a;&#xff08;1&#xff09;在ALOHA…...

MFC 抛体运动模拟:常见问题解决与界面美化

在 MFC 中开发抛体运动模拟程序时,我们常遇到 轨迹残留、无效刷新、视觉单调、物理逻辑瑕疵 等问题。本文将针对这些痛点,详细解析原因并提供解决方案,同时兼顾界面美化,让模拟效果更专业、更高效。 问题一:历史轨迹与小球残影残留 现象 小球运动后,历史位置的 “残影”…...

DingDing机器人群消息推送

文章目录 1 新建机器人2 API文档说明3 代码编写 1 新建机器人 点击群设置 下滑到群管理的机器人&#xff0c;点击进入 添加机器人 选择自定义Webhook服务 点击添加 设置安全设置&#xff0c;详见说明文档 成功后&#xff0c;记录Webhook 2 API文档说明 点击设置说明 查看自…...

基于Springboot+Vue的办公管理系统

角色&#xff1a; 管理员、员工 技术&#xff1a; 后端: SpringBoot, Vue2, MySQL, Mybatis-Plus 前端: Vue2, Element-UI, Axios, Echarts, Vue-Router 核心功能&#xff1a; 该办公管理系统是一个综合性的企业内部管理平台&#xff0c;旨在提升企业运营效率和员工管理水…...

华为OD机试-最短木板长度-二分法(A卷,100分)

此题是一个最大化最小值的典型例题&#xff0c; 因为搜索范围是有界的&#xff0c;上界最大木板长度补充的全部木料长度&#xff0c;下界最小木板长度&#xff1b; 即left0,right10^6; 我们可以设置一个候选值x(mid)&#xff0c;将木板的长度全部都补充到x&#xff0c;如果成功…...

uniapp 实现腾讯云IM群文件上传下载功能

UniApp 集成腾讯云IM实现群文件上传下载功能全攻略 一、功能背景与技术选型 在团队协作场景中&#xff0c;群文件共享是核心需求之一。本文将介绍如何基于腾讯云IMCOS&#xff0c;在uniapp中实现&#xff1a; 群内文件上传/下载文件元数据管理下载进度追踪跨平台文件预览 二…...