Diferencia entre revisiones de «Mongo DB»
| Línea 87: | Línea 87: | ||
required: [true, "No hay introducido ningún nombre"] | required: [true, "No hay introducido ningún nombre"] | ||
</syntaxhighlight> | </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> | ||
| + | FotoDos.updateOne( | ||
| + | { _id: "5f5a1cc3be03c7105c6a739c" }, | ||
| + | { title: "Nombre updateOne" }, | ||
| + | function (err) { | ||
| + | if (err) { | ||
| + | console.log(err); | ||
| + | } else { | ||
| + | console.log("Se actualizaron los datos Correctamente") | ||
| + | }}) | ||
| + | </syntaxhighlight> | ||
| + | ====Borrar datos==== | ||
Revisión del 15:01 10 sep 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}})
Sumario
8. Working with The Native MongoDB Driver
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
</syntaxhighlight>
FotoDos.updateOne(
{ _id: "5f5a1cc3be03c7105c6a739c" },
{ title: "Nombre updateOne" },
function (err) {
if (err) {
console.log(err);
} else {
console.log("Se actualizaron los datos Correctamente")
}})
</syntaxhighlight>