Mongodb 按字段值[平均值]删除
我正在学习平均堆栈。我想执行Mongodb 按字段值[平均值]删除,mongodb,typescript,api,mongoose,mean-stack,Mongodb,Typescript,Api,Mongoose,Mean Stack,我正在学习平均堆栈。我想执行CRUD操作,我正在使用mongoose。我下面是关于stackoverflow的问题。我想按特定值删除文档。在我的例子中,它是一篇带有唯一articleid的文章,应该删除它。不知不觉地,我在参数上犯了一些可怕的错误。请纠正我 mongodb中的示例文档 { _id: objectId("5d77de7ff5ae9e27bd787bd6"), articleid:"art5678", title:"Installing JDK 8 in Ubuntu 1
CRUD
操作,我正在使用mongoose
。我下面是关于stackoverflow的问题。我想按特定值删除文档。在我的例子中,它是一篇带有唯一articleid
的文章,应该删除它。不知不觉地,我在参数上犯了一些可怕的错误。请纠正我
mongodb
中的示例文档
{
_id: objectId("5d77de7ff5ae9e27bd787bd6"),
articleid:"art5678",
title:"Installing JDK 8 in Ubuntu 18.04 and later",
content:"<h2>Step 1: Add repository</h2><p><strong>$ sudo add-apt-repository pp..."
date:"Tue, 10 Sep 2019 17:33:51 GMT"
contributor:"Tanzeel Mirza",
__v:0
}
我已经创建了一个servicearticle.service.ts
import { Component, OnInit } from '@angular/core';
import { ArticleService } from '../article.service';
@Component({
selector: 'app-articles',
templateUrl: './articles.component.html',
styleUrls: ['./articles.component.css']
})
export class ArticlesComponent implements OnInit {
articles = []
constructor(private _articleService: ArticleService) { }
ngOnInit() {
this._articleService.getEvents()
.subscribe(
res => this.articles = res,
err => console.log(err)
)
}
onPress(id) {
this._articleService.deleteArticle()
.subscribe (
data => {
console.log("hello");
}
);
}
}
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ArticleService {
private _deleteUrl = "http://localhost:3000/api/delete/:id";
constructor(private http: HttpClient) { }
getAllArticles() {
...
}
deleteArticle(id) {
return this.http.delete<any>(this._deleteUrl);
}
}
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Article = require('../models/article');
const dbstring = ...
mongoose.connect(dbstring, { useNewUrlParser: true }, err => {
...
})
router.delete('/delete/:id', (req, res) => {
let articleData=req.params.id;
console.log(articleData); //Output: {}
console.log('on delete url '+articleData); //Output: on delete url undefined
Article.deleteOne({articleid: articleData}, (error, article) => {
if(error) {
console.log(error)
}
else {
if(!article) {
res.status(401).send('Something went wrong')
}
else {
//res.json(article);
}
}
})
})
module.exports = router;
router.delete('/delete/:id', (req, res) => {
let articleId=req.params.id;
Article.deleteOne({articleid: articleId}, (error, article) => {
if(error) {
console.log(error)
}
else {
if(!article) {
...
}
else {
...
}
}
})
})
好的,不要为我写代码,但请至少告诉我一些学习材料。好的。我做了越来越多的研究,解决了这个问题。这些是变化
api.js
import { Component, OnInit } from '@angular/core';
import { ArticleService } from '../article.service';
@Component({
selector: 'app-articles',
templateUrl: './articles.component.html',
styleUrls: ['./articles.component.css']
})
export class ArticlesComponent implements OnInit {
articles = []
constructor(private _articleService: ArticleService) { }
ngOnInit() {
this._articleService.getEvents()
.subscribe(
res => this.articles = res,
err => console.log(err)
)
}
onPress(id) {
this._articleService.deleteArticle()
.subscribe (
data => {
console.log("hello");
}
);
}
}
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ArticleService {
private _deleteUrl = "http://localhost:3000/api/delete/:id";
constructor(private http: HttpClient) { }
getAllArticles() {
...
}
deleteArticle(id) {
return this.http.delete<any>(this._deleteUrl);
}
}
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Article = require('../models/article');
const dbstring = ...
mongoose.connect(dbstring, { useNewUrlParser: true }, err => {
...
})
router.delete('/delete/:id', (req, res) => {
let articleData=req.params.id;
console.log(articleData); //Output: {}
console.log('on delete url '+articleData); //Output: on delete url undefined
Article.deleteOne({articleid: articleData}, (error, article) => {
if(error) {
console.log(error)
}
else {
if(!article) {
res.status(401).send('Something went wrong')
}
else {
//res.json(article);
}
}
})
})
module.exports = router;
router.delete('/delete/:id', (req, res) => {
let articleId=req.params.id;
Article.deleteOne({articleid: articleId}, (error, article) => {
if(error) {
console.log(error)
}
else {
if(!article) {
...
}
else {
...
}
}
})
})
和article.service.ts
private\u deleteUrl=”http://localhost:3000/api/delete";代码>
deleteArticle
方法应为
deleteArticle(id) {
return this.http.delete<any>(this._deleteUrl+'/'+id);
}
deleteArticle(id){
返回this.http.delete(this.\u deleteUrl+'/'+id);
}