How to deal with document-oriented data

2024-06-18 23:08
文章标签 data document oriented deal

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1073316

相关文章

论文翻译:arxiv-2024 Benchmark Data Contamination of Large Language Models: A Survey

Benchmark Data Contamination of Large Language Models: A Survey https://arxiv.org/abs/2406.04244 大规模语言模型的基准数据污染:一项综述 文章目录 大规模语言模型的基准数据污染:一项综述摘要1 引言 摘要 大规模语言模型(LLMs),如GPT-4、Claude-3和Gemini的快

CentOS下mysql数据库data目录迁移

https://my.oschina.net/u/873762/blog/180388        公司新上线一个资讯网站,独立主机,raid5,lamp架构。由于资讯网是面向小行业,初步估计一两年内访问量压力不大,故,在做服务器系统搭建的时候,只是简单分出一个独立的data区作为数据库和网站程序的专区,其他按照linux的默认分区。apache,mysql,php均使用yum安装(也尝试

JavaScript中document.cookie

“某些 Web 站点在您的硬盘上用很小的文本文件存储了一些信息,这些文件就称为 Cookie。”—— MSIE 帮助。一般来说,Cookies 是 CGI 或类似,比 HTML 高级的文件、程序等创建的,但是 javascript 也提供了对 Cookies 的很全面的访问权利。       每个 Cookie 都是这样的:<cookie名>=<值>   <cookie名>的限制与 javasc

使用Spring Boot集成Spring Data JPA和单例模式构建库存管理系统

引言 在企业级应用开发中,数据库操作是非常重要的一环。Spring Data JPA提供了一种简化的方式来进行数据库交互,它使得开发者无需编写复杂的JPA代码就可以完成常见的CRUD操作。此外,设计模式如单例模式可以帮助我们更好地管理和控制对象的创建过程,从而提高系统的性能和可维护性。本文将展示如何结合Spring Boot、Spring Data JPA以及单例模式来构建一个基本的库存管理系统

15 组件的切换和对组件的data的使用

划重点 a 标签的使用事件修饰符组件的定义组件的切换:登录 / 注册 泡椒鱼头 :微辣 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-

12C 新特性,MOVE DATAFILE 在线移动 包括system, 附带改名 NID ,cdb_data_files视图坏了

ALTER DATABASE MOVE DATAFILE  可以改名 可以move file,全部一个命令。 resue 可以重用,keep好像不生效!!! system照移动不误-------- SQL> select file_name, status, online_status from dba_data_files where tablespace_name='SYSTEM'

SIGMOD-24概览Part7: Industry Session (Graph Data Management)

👇BG3: A Cost Effective and I/O Efficient Graph Database in ByteDance 🏛机构:字节 ➡️领域: Information systems → Data management systemsStorage management 📚摘要:介绍了字节新提出的ByteGraph 3.0(BG3)模型,用来处理大规模图结构数据 背景

java.sql.SQLException: No data found

Java代码如下: package com.accord.utils;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import

FORM的ENCTYPE=multipart/form-data 时request.getParameter()值为null问题的解决

此情况发生于前台表单传送至后台java servlet处理: 问题:当Form需要FileUpload上传文件同时上传表单其他控件数据时,由于设置了ENCTYPE=”multipart/form-data” 属性,后台request.getParameter()获取的值为null 上传文件的参考代码:http://www.runoob.com/jsp/jsp-file-uploading.ht

Oracle Data Guard:Oracle数据库的高可用性和灾难恢复解决方案

在企业级数据库管理中,确保数据的高可用性和在灾难情况下的快速恢复是至关重要的。Oracle Data Guard是Oracle公司提供的一种强大的数据库高可用性解决方案,它通过在主数据库和至少一个备用数据库之间提供实时或近实时的数据保护来实现这一目标。本文将详细介绍如何在Oracle数据库中部署和使用Oracle Data Guard,包括其基本概念、配置步骤、管理技巧和实际应用示例。 1. O