Diferencia entre revisiones de «Mongo DB»
| (No se muestran 11 ediciones intermedias del mismo usuario) | |||
| Línea 23: | Línea 23: | ||
db.products.updateOne({_id: 1}, {$set: {stock: 32}}) | db.products.updateOne({_id: 1}, {$set: {stock: 32}}) | ||
====8. Working with The Native MongoDB Driver==== | ====8. Working with The Native MongoDB Driver==== | ||
| + | =Relaciones= | ||
http://mongodb.github.io/node-mongodb-native/3.5/quick-start/quick-start/ | http://mongodb.github.io/node-mongodb-native/3.5/quick-start/quick-start/ | ||
| Línea 34: | Línea 35: | ||
====Scheme==== | ====Scheme==== | ||
https://docs.mongodb.com/realm/mongodb/document-schemas/ | https://docs.mongodb.com/realm/mongodb/document-schemas/ | ||
| + | Type | ||
* Arrays | * Arrays | ||
* Objets | * Objets | ||
| Línea 39: | Línea 41: | ||
* Numbers | * Numbers | ||
* Booleans | * Booleans | ||
| + | bsonType | ||
| + | * objectId | ||
| + | * int | ||
| + | * long | ||
| + | * double | ||
| + | * decimal | ||
| + | * date | ||
| + | * timestamp | ||
| + | * regex | ||
| + | https://docs.mongodb.com/manual/reference/operator/query/type/#document-type-available-types | ||
| + | |||
| + | Todos los metodos | ||
| + | |||
| + | https://mongoosejs.com/docs/api/model.html | ||
| + | ====Buscar y devolver ==== | ||
| + | Busca todos los items de una collection y devuelve solo el titulo | ||
| + | <syntaxhighlight lang="js"> | ||
| + | Foto.find(function(err, items) { | ||
| + | if (err) { | ||
| + | console.log(err) | ||
| + | } else { | ||
| + | // console.log(items) | ||
| + | items.forEach(function(item){ | ||
| + | console.log(item.title) | ||
| + | }) | ||
| + | } | ||
| + | }); | ||
| + | </syntaxhighlight> | ||
| + | ====Validació datos==== | ||
| + | https://mongoosejs.com/docs/schematypes.html | ||
| + | |||
| + | En la creación del Schema, se crea un objeto con los parametros necesarios. | ||
| + | <syntaxhighlight lang="js"> | ||
| + | name: String, | ||
| + | rating: { | ||
| + | type: Number, | ||
| + | min: 1, | ||
| + | max: 10 | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | lo utilizaremos para confirmar la introducción de los datos. | ||
| + | <syntaxhighlight lang="js"> | ||
| + | name: { | ||
| + | type: String, | ||
| + | required: [true, "No hay introducido ningún nombre"] | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | |||
| + | === Todo los métodos=== | ||
| + | '''https://mongoosejs.com/docs/api/model.html''' | ||
| + | ====Actualizar datos==== | ||
| + | https://mongoosejs.com/docs/api/model.html#model_Model.updateOne | ||
| + | <syntaxhighlight lang="js"> | ||
| + | FotoDos.updateOne( | ||
| + | { _id: "5f5a1cc3be03c7105c6a739c" }, | ||
| + | { title: "Nombre updateOne" }, | ||
| + | function (err) { | ||
| + | if (err) { | ||
| + | console.log(err); | ||
| + | } else { | ||
| + | mongoose.connection.close() // cerrar la conexion con mongodb | ||
| + | console.log("Se actualizaron los datos Correctamente") | ||
| + | }}) | ||
| + | </syntaxhighlight> | ||
| + | ====Borrar datos==== | ||
| + | Todos los registros que tiene la fecha 200-01-03 | ||
| + | <syntaxhighlight lang="js"> | ||
| + | FotoDos.deleteMany( | ||
| + | { date: "2000-01-03 00:00:00.000Z" }, | ||
| + | function (err) { | ||
| + | if (err) { | ||
| + | console.log(err); | ||
| + | } else { | ||
| + | mongoose.connection.close() // cerrar la conexion con mongodb | ||
| + | console.log("Se borraron todos los registros") | ||
| + | }}) </syntaxhighlight> | ||
| + | ==Query operadores de comparación== | ||
| + | ===$eq=== | ||
| + | Igual a | ||
| + | { <field>: { $eq: <value> } } | ||
| + | db.inventory.find( { cantidad: { $eq: 20 } } ) | ||
| + | ===$gt=== | ||
| + | mas grande que | ||
| + | db.inventory.find( { qty: { $gt: 20 } } ) | ||
| + | ===$gte=== | ||
| + | mas grande o igual a | ||
| + | db.inventory.find( { qty: { $gte: 20 } } ) | ||
| + | ===$in=== | ||
| + | igual que dentro de los datos de una array | ||
| + | db.inventory.find( { qty: { $in: [ 5, 15 ] } } ) | ||
| + | ===$it=== | ||
| + | menor que | ||
| + | db.inventory.find( { qty: { $lt: 20 } } ) | ||
| + | ===$ite=== | ||
| + | menor que o igual | ||
| + | db.inventory.find( { qty: { $lte: 20 } } ) | ||
| + | ===$ne=== | ||
| + | no es igual | ||
| + | db.inventory.find( { qty: { $ne: 20 } } ) | ||
| + | ===$nin=== | ||
| + | no es especifico en una array o no existe | ||
| + | db.inventory.find( { qty: { $nin: [ 5, 15 ] } } ) | ||
Revisión actual del 19:26 5 oct 2020
https://docs.mongodb.com/manual/crud/
show collections
db.products.find()
db.inventory.insertOne(
{ "item" : "canvas",
"qty" : 100,
"tags" : ["cotton"],
"size" : { "h" : 28, "w" : 35.5, "uom" : "cm" }
}
)
db.collection.find() https://docs.mongodb.com/manual/reference/method/db.collection.find/#db.collection.find
https://docs.mongodb.com/manual/reference/operator/query/
busca los que tienen mas de 5 en stock
db.products.find({stock: {$gt: 5}})
Modificar un objeto
db.products.updateOne({_id: 1}, {$set: {stock: 32}})
8. Working with The Native MongoDB Driver
Relaciones
http://mongodb.github.io/node-mongodb-native/3.5/quick-start/quick-start/
http://mongodb.github.io/node-mongodb-native/3.5/api/
Ejecutar la conexión
node App.js
Mongoose
Scheme
https://docs.mongodb.com/realm/mongodb/document-schemas/ Type
- Arrays
- Objets
- Strings
- Numbers
- Booleans
bsonType
- objectId
- int
- long
- double
- decimal
- date
- timestamp
- regex
https://docs.mongodb.com/manual/reference/operator/query/type/#document-type-available-types
Todos los metodos
https://mongoosejs.com/docs/api/model.html
Buscar y devolver
Busca todos los items de una collection y devuelve solo el titulo
Foto.find(function(err, items) {
if (err) {
console.log(err)
} else {
// console.log(items)
items.forEach(function(item){
console.log(item.title)
})
}
});
Validació datos
https://mongoosejs.com/docs/schematypes.html
En la creación del Schema, se crea un objeto con los parametros necesarios.
name: String,
rating: {
type: Number,
min: 1,
max: 10
}
lo utilizaremos para confirmar la introducción de los datos.
name: {
type: String,
required: [true, "No hay introducido ningún nombre"]
Todo los métodos
https://mongoosejs.com/docs/api/model.html
Actualizar datos
https://mongoosejs.com/docs/api/model.html#model_Model.updateOne
FotoDos.updateOne(
{ _id: "5f5a1cc3be03c7105c6a739c" },
{ title: "Nombre updateOne" },
function (err) {
if (err) {
console.log(err);
} else {
mongoose.connection.close() // cerrar la conexion con mongodb
console.log("Se actualizaron los datos Correctamente")
}})
Borrar datos
Todos los registros que tiene la fecha 200-01-03
FotoDos.deleteMany(
{ date: "2000-01-03 00:00:00.000Z" },
function (err) {
if (err) {
console.log(err);
} else {
mongoose.connection.close() // cerrar la conexion con mongodb
console.log("Se borraron todos los registros")
}})
Query operadores de comparación
$eq
Igual a { <field>: { $eq: <value> } }
db.inventory.find( { cantidad: { $eq: 20 } } )
$gt
mas grande que
db.inventory.find( { qty: { $gt: 20 } } )
$gte
mas grande o igual a
db.inventory.find( { qty: { $gte: 20 } } )
$in
igual que dentro de los datos de una array
db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
$it
menor que
db.inventory.find( { qty: { $lt: 20 } } )
$ite
menor que o igual
db.inventory.find( { qty: { $lte: 20 } } )
$ne
no es igual
db.inventory.find( { qty: { $ne: 20 } } )
$nin
no es especifico en una array o no existe
db.inventory.find( { qty: { $nin: [ 5, 15 ] } } )